我对 NHibernate 有很大的疑问。我有一个从 Table1 到 Table2 的引用,我希望 NHibernate 在 Table2 中找不到相应的记录时,不要再针对 Table2 发出 SELECT 语句,我不知道,真的真的确定它实际上不存在。
我已经尝试在我的引用中添加像 .LazyLoad(Laziness.False) 和 .NotFound.Ignore() 这样的修饰符,但是 NHibernate 以极端的方式愉快地忽略了我的命令偏见,发布它的选择并破坏我的代码。
请您参考如下方法:
NHibernate 尝试加载“不存在”是正确的。它必须这样做。
如此处所述Ayende - NHibernate Mapping (摘录):
12) not-found is another legacy feature, it controls how NHibernate behaves when it finds an invalid foreign key. That is, a value that points to an entity that doesn’t exist. By default, this would trigger an error, as this generally indicate a problem with the database, but with legacy database, you can tell it to set the property value to null instead.
可以在这里找到:Lazy loading for NHibernate with Ignore.NotFound (摘录):
When you specify the .NotFound().Ignore() this forces the entity to be eagerly loaded and cannot be overriden with the .LazyLoad(). NHibernate does this because it has to be sure that relationship exists or doesn't exist since you are not relying on the database to enforce this.
这里Why Nhibernate won't lazy load my many-to-one relationship? , José F. Romaniello 说:
This is your problem, nhibernate must to be sure, that an invoice EXIST or do not exist for each enrollment.
I'd strongly recommend you to fix your data problems and remove the not-found="ignore" attribute. It is a bad thing.
