简体   繁体   English

Nhibernate Session.Evict()是否必须对代理对象起作用? (或者是否有其他方法克隆持久对象图?)

[英]Must Nhibernate Session.Evict() act on a proxy object? (Or is there an alternative way to clone a persisted object graph?)

I am trying to create a clone of a persisted object graph and it seems like Session.Evict(PersistedObject) is the way to do this. 我正在尝试创建持久对象图的克隆,而Session.Evict(PersistedObject)似乎是实现此目的的方法。 By removing the instance from the Session cache, I can then set persist it as a new "cloned" record. 通过从会话缓存中删除实例,我可以将其设置为新的“克隆”记录。

I have tried three approaches and each has been unsuccessful. 我尝试了三种方法,但每种方法均未成功。

  1. Use eager loading to get my object graph from the db and attempt to Evict it. 使用紧急加载从数据库获取我的对象图,然后尝试逐出它。 This results in a KeyNotFoundException 这导致KeyNotFoundException
  2. Use Session.Load(objectId) and attempt to evict it. 使用Session.Load(objectId)并尝试将其逐出。 Evict works, but I only have a proxy to work with and not the hydrated object I need 驱逐有效,但我只有一个代理可以使用,而没有我需要的水合物体
  3. Use Session.Get(objectId) and attempt to Evict it. 使用Session.Get(objectId)并尝试逐出它。 This results in a KeyNotFoundException 这导致KeyNotFoundException

I've struggled to find any real documentation or examples on this subject. 我一直在努力寻找有关此主题的任何真实文档或示例。 I've found some that come close, but nothing that really explains where I'm going wrong 我找到了一些接近的东西,但没有什么能真正解释我要去哪里的问题

With regards to the last link, I have checked my Equals and GetHashCode methods, but they do not get hit when calling Evict. 关于最后一个链接,我已经检查了我的Equals和GetHashCode方法,但是在调用Evict时它们没有被击中。 Also, Session.Contains(objectToBeEvicted) returns true just prior to the Evict() 另外,Session.Contains(objectToBeEvicted)在Evict()之前返回true

As for the second question in the title - is there a better way to approach this problem? 至于标题中的第二个问题-是否有更好的方法来解决此问题? This can't be such a rare scenario 这不是一个罕见的情况

Thanks in advance 提前致谢

To answer the initial question: 要回答最初的问题:

No, Evict() is not limited to acting on proxies, it can remove any object from the session. 不, Evict()不仅限于代理,它可以从会话中删除任何对象。 If you are getting some sort of exception, most likely you are doing something wrong, but the question does not contain enough information to figure out what. 如果您遇到某种异常,则很可能是您做错了什么,但问题所包含的信息不足以找出问题所在。

尝试对对象进行深度克隆( .Net深度克隆-这样做的最佳方法是什么? ),然后将其添加到会话中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM