NHibernate的删除对象将被重新保存通过级联 [英] NHibernate Deleted object would be re-saved by cascade
问题描述
在我的解决方案我有关联的业务对象的一个分支。所以,当我尝试保存根对象处理后,我得到消息的异常已删除对象通过级联将被重新保存。什么是指删除对象后,其仍处于收藏,其他协会等存在。请问有人知道怎么去删除的对象引用的列表。
它很难找到没有调试器支持引用。
In my solution I have a branch of business objects with associations. So when I try save a root object after processing I get an exception with message "Deleted object would be re-saved by cascade". What means that after deleting of an object its still exists in collections, other association and so on. Does somebody knows how to get a list of references to the deleted object. Its very difficult to find references without debugger support.
推荐答案
最常见的情况的(我的经验) 的是有有一些配对/中对象的集合两根对象。
The most common scenario (my experience) is having two root objects having collections of some pairing/middle object.
public class Employee
{
public virtual IList<Occupation> Occupations { get; set; }
}
public class Company
{
public virtual IList<Occupation> Occupations { get; set; }
}
现在,我们有职业
像这样
public class Occupation
{
public virtual Employee Employee { get; set; }
public virtual Company Company { get; set; }
}
那么,会发生什么:
So, what could happen:
- 我们删除职业从
employee.Occupations
集合。 - 在此交易,工作单位,我们也tuch,因此加载在
公司
- 公司被启动。其职业的收集得到加载。因此,参考去掉职业保持有
- NHibernate的说:
通过级联将被重新保存已删除的对象
- we remove an Occupation from
employee.Occupations
collection. - During that transaction, unit of work, we also tuch and therefore load the
Company
- Company gets initiated. Its collection of Occupations get loaded. So the reference to removed Occupation is kept there
- NHibernate says:
Deleted object would be re-saved by cascade
解决方案(S):
- 确保该公司不会装载(保持为代理)
- 或
删除()
的职业也从company.Occupations
- 请不要对公司方使用这样的映射:
- be sure that the Company is never loaded (stays as proxy)
- or
Remove()
the Occupation also fromcompany.Occupations
- do not use mapping like this on Company side:
的(做的不可以使用级联)的
(do not use the cascade)
<bag name="Occupations" lazy="true" inverse="true" batch-size="25"
cascade="all-delete-orphan">
// this above setting on Company side is making issues...
<key column="Company_ID" />
<one-to-many class="Occupation" />
</bag>
这篇关于NHibernate的删除对象将被重新保存通过级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!