代码之家  ›  专栏  ›  技术社区  ›  David

如何在Fluent NHibernate中启用LazyLoad?

  •  1
  • David  · 技术社区  · 14 年前

    雇员

    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);
        }
    }
    

    员工地图

    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");
        }
    }
    

    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 回复  |  直到 14 年前
        1
  •  4
  •   Diego Mijelshon    14 年前

        2
  •  -2
  •   rebelliard    14 年前

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

    public EmployeeMap()
    {
        Table("Employees");
        LazyLoad();
    
        // etc.
    }
    

    也就是说,使用foreach语句进行测试可能是有害的,因为即使启用了延迟加载,在查询“employee.FirstName”时,NHibernate也会命中数据库并返回结果。最好是使用NHibernate生成的SQL,或者只使用 NHibernate Profiler .