有外键但被引用的行不存在时,是否可以避免NHibernate.ObjectNotFoundException? [英] Is it possible to avoid NHibernate.ObjectNotFoundException when there is a foreign key but the referenced row does not exist?
问题描述
我正在使用NHibernate从旧版数据库中提取一些数据,我发现有几种情况存在外键,但是引用的行已被删除.
I am using NHibernate to pull some data out of a legacy db, and I have found several cases where there is a foreign key, but the referenced row has been removed.
当我进行NHibernate映射时(像这样使用Fluent NHibernate:
When I make my NHibernate mapping (using Fluent NHibernate like so:
References(d => d.Group)
.WithColumns("groupId", "dataset")
.SetAttribute("lazy", "true");
在加载根对象时,我得到了Group
的统一代理,而在尝试使用它时,又得到了ObjectNotFoundException
.
I get an unitialized proxy for Group
when loading the root object, followed by an ObjectNotFoundException
when I attempt to use it.
如果禁用延迟加载,则在加载根目录时会立即得到ObjectNotFoundException
.
If I disable lazy loading, I get an ObjectNotFoundException
immediately when loading the root.
因此:有没有办法让NHibernate在加载根目录时使Group
为null?还是有可能以某种方式检查统一代理,以查看它是否将成功加载行?
Therefore: Is there a way to have NHibernate make the Group
null when loading the root? Or is it possible to check the unitialized proxy somehow, to see if it will succeed in loading the row?
推荐答案
I found the solution here - i needed to add
.SetAttribute("not-found", "ignore");
到映射.
这篇关于有外键但被引用的行不存在时,是否可以避免NHibernate.ObjectNotFoundException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!