C# NHibernate Session.Evict()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/679474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NHibernate Session.Evict()
提问by lomaxx
I don't have access to code here in front of me so I was just wondering if someone could help me out with Session.Evict().
我无法访问我面前的代码,所以我只是想知道是否有人可以帮助我使用 Session.Evict()。
Say I have a Person object with a child collection of Addresses. I populate the Person object from a session and lazy load the Addresses collection. I then call Session.Evict(personObject) to detach the Person object from the session. My question is, if I attempt to access the Addresses collection will it just return null, or will I get an exception because the NHibernate proxy can't find the associated session?
假设我有一个带有子地址集合的 Person 对象。我从会话填充 Person 对象并延迟加载 Addresses 集合。然后我调用 Session.Evict(personObject) 从会话中分离 Person 对象。我的问题是,如果我尝试访问 Addresses 集合,它会返回 null,还是会因为 NHibernate 代理找不到关联的会话而得到异常?
采纳答案by Danielg
If you cause the lazy load to happen before you evict the entity, then the collection will be accessible even after the eviction. However if you evict the entity and then try to lazy load the child collection you will get an exception.
如果在驱逐实体之前导致延迟加载发生,那么即使在驱逐之后也可以访问该集合。但是,如果您驱逐实体,然后尝试延迟加载子集合,则会出现异常。
回答by Damovisa
I also don't have code in front of me, but from memory, you'll probably get an exception.
我也没有代码在我面前,但从记忆中,你可能会得到一个例外。
If you have lazy loading on and working, NHibernate will try to load the Addresses collection. It should never return an incorrect value/collection, it'll only complain when it can't load what's been asked.
如果您有延迟加载和工作,NHibernate 将尝试加载地址集合。它永远不应该返回不正确的值/集合,它只会在无法加载请求的内容时抱怨。
回答by Luke
You will receive a NHibernate.LazyInitializationException.
您将收到一个 NHibernate.LazyInitializationException。