流畅的 NHibernate +禁用延迟加载
嘿,我尝试在我的集合上禁用 LazyLoad,但没有运气...到目前为止我尝试过的代码是:
// Person.cs
public class Person
{
public virtual int Id { get; private set; }
public virtual string FirstName { get; set; }
public virtual IList<Car> Cars { get; set; }
public Person()
{
Cars = new List<Car>();
}
public virtual void AddCar(Car car)
{
Cars.Add(car);
}
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.Id);
Map(x => x.FirstName);
HasMany(x => x.Cars).KeyColumn("PersonId").Cascade.AllDeleteOrphan().Not.LazyLoad();
Table("Persons");
}
}
// Car.cs
public class Car
{
public virtual int Id { get; set; }
public virtual string CarName { get; set; }
public virtual Person Person { get; set; }
}
public class CarMap : ClassMap<Car>
{
public CarMap()
{
Id(x => x.Id);
Map(x => x.CarName);
HasOne(x => x.Person).Not.LazyLoad();
Table("Cars");
}
}
有什么建议吗?
提前致谢!
Hey there, I've tried to disable LazyLoad on my collections without luck... The code I've tried so far is:
// Person.cs
public class Person
{
public virtual int Id { get; private set; }
public virtual string FirstName { get; set; }
public virtual IList<Car> Cars { get; set; }
public Person()
{
Cars = new List<Car>();
}
public virtual void AddCar(Car car)
{
Cars.Add(car);
}
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.Id);
Map(x => x.FirstName);
HasMany(x => x.Cars).KeyColumn("PersonId").Cascade.AllDeleteOrphan().Not.LazyLoad();
Table("Persons");
}
}
// Car.cs
public class Car
{
public virtual int Id { get; set; }
public virtual string CarName { get; set; }
public virtual Person Person { get; set; }
}
public class CarMap : ClassMap<Car>
{
public CarMap()
{
Id(x => x.Id);
Map(x => x.CarName);
HasOne(x => x.Person).Not.LazyLoad();
Table("Cars");
}
}
Any suggestions?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
延迟加载意味着仅当您访问 cars 属性时才会加载 cars 集合。因此,当您关闭此功能时,在装载一个人时,您也将始终装载他的所有汽车。要查看这是否对您“有效”,您可以打开 SQL Profiler(或获取 NHibernate profiler 的试用版),并调试您的代码。当您单步执行代码时,您可以看到汽车集合是否立即加载,或者仅在您第一次访问时加载。
简而言之,我不认为你做错了什么,只是误解了这个概念。
Lazy loading means that the cars collection will be loaded only when you access the cars property. So when you turn this off, when loading a person you will always also load all his cars. To see if this "works" for you, you can open up SQL profiler (or grab the trial for NHibernate profiler), and debug your code. When you step through your code you can see if the cars collection is loaded right away, or only when you first access it.
In short, I don't think you've done something wrong, just misunderstood the concept.
如果您使用的是
Load
方法,请尝试使用Get
方法。Load
始终返回代理并延迟加载 iirc。但要注意 SELECT N+1。编辑:重新阅读其他评论后,它可能会按预期工作。 Doron 已经解释了延迟加载应该如何工作。因此,如果您不想将所有汽车与一个人一起加载,则应启用延迟加载。默认情况下已启用延迟加载,因为如果您急切地加载所有集合,这将导致 SELECT N+1,如前所述。即人选1,车选N。这种行为通常是不需要的,因此我发出警告:) 延迟加载意味着只有在明确要求时才会加载集合。
If you are using the
Load
method try theGet
instead.Load
always returns a proxy and loads lazily iirc. Beware of SELECT N+1 though.EDIT: After re-reading other comments it probably works as expected. Doron already explained how is lazy loading supposed to work. So if you do NOT want to load all the cars together with a person, then lazy loading should be enabled. Lazy loading is already enabled by default, because if you eagerly load all your collections, this results in a SELECT N+1, as mentioned before. That is, one select for the person and N for cars. Such behavior is usually not desired, hence my warning :) Lazy loading means that collections will be loaded only if explicitly asked for.