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; }
}
现在,我们有这样的Occupation
public class Occupation
{
public virtual Employee Employee { get; set; }
public virtual Company Company { get; set; }
}
那么,会发生什么:
- 我们从
employee.Occupations
集合中删除了 Occupation. - 在该交易中,工作单元,我们也触摸并因此加载
Company
- 公司成立.它的职业集合被加载.所以对移除的职业的引用保留在那里
- 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
解决方案:
- 确保公司永远不会被加载(作为代理)
- 或
Remove()
职业也来自company.Occupations
- 不要在公司端使用这样的映射:
(不要不要使用级联)
<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屋!