如何在Fluent NHibernate中启用LazyLoad?

时间:2010-07-30 01:58:55

标签: nhibernate fluent-nhibernate

我正在使用NorthWind数据库测试Fluent NHibernate。现在,我已经创建了Employee和EmployeeMap类。源代码如下所示。

班级员工

public class Employee
{
    public virtual int EmployeeID { get; private set; }
    public virtual string LastName { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string Title { get; set; }
    public virtual string TitleOfCourtesy { get; set; }
    public virtual DateTime? BirthDate { get; set; }
    public virtual DateTime? HireDate { get; set; }
    public virtual string Address { get; set; }
    public virtual string City { get; set; }
    public virtual string Region { get; set; }
    public virtual string PostalCode { get; set; }
    public virtual string Country { get; set; }
    public virtual string HomePhone { get; set; }
    public virtual string Extension { get; set; }
    public virtual string Notes { get; set; }
    public virtual Employee ReportsTo { get; set; }
    public virtual string PhotoPath { get; set; }

    public virtual IList<Territory> Territories{ get; set; }

    public Employee()
    {
        Territories = new List<Territory>();
    }

    public virtual void AddTerritory(Territory territory)
    {
        territory.Employees.Add(this);
        this.Territories.Add(territory);
    }
}

EmployeeMap

public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        Table("Employees");

        Id(x => x.EmployeeID);
        Map(x => x.LastName);
        Map(x => x.FirstName);
        Map(x => x.Title);
        Map(x => x.TitleOfCourtesy);
        Map(x => x.BirthDate);
        Map(x => x.HireDate);
        Map(x => x.Address);
        Map(x => x.City);
        Map(x => x.Region);
        Map(x => x.PostalCode);
        Map(x => x.Country);
        Map(x => x.HomePhone);
        Map(x => x.Extension);
        Map(x => x.Notes);
        Map(x => x.PhotoPath);
        References(x => x.ReportsTo).Column("ReportsTo").LazyLoad();

        HasManyToMany(x => x.Territories)
        .Cascade.All()
        .Table("EmployeeTerritories")
        .ParentKeyColumn("EmployeeID")
        .ChildKeyColumn("TerritoryID");
    }
}

然后我尝试从数据库加载所有员工,但所有员工都在ReportsTo属性上有引用对象。

var sessionFactory = CreateSessionFactory();

using (var session = sessionFactory.OpenSession())
{
    using (session.BeginTransaction())
    {
        Console.WriteLine("All employees");

        var emp_ = session.CreateCriteria(typeof(Employee));
        var employees = emp_.List<Employee>();

        foreach (var employee in employees)
        {
            Console.WriteLine(employee.FirstName); // every employee has reference object on ReportsTo property here.
        }

        Console.Write("--------");
    }
}

我想知道,我的代码有什么问题以及如何修复它?

2 个答案:

答案 0 :(得分:4)

默认情况下启用延迟加载。 ReportsTo中的引用是一个代理,只有在使用ID以外的任何属性时才会从数据库加载。

答案 1 :(得分:-2)

默认情况下不启用延迟加载。

public EmployeeMap()
{
    Table("Employees");
    LazyLoad();

    // etc.
}

话虽如此,使用foreach语句进行测试可能是恶作剧的,因为即使您启用了延迟加载,第二次查询“employee.FirstName”,NHibernate也会命中数据库并返回结果。你最好去捕捉NHibernate生成的SQL或只使用NHibernate Profiler