Fluent NHibernate级联删除问题
我在流畅的 nhibernate 级联删除方面遇到问题。我确信我做错了什么,因为它不起作用。
这是我的对象:
public class Parent
{
public int Id { get; set; }
public IList<SequencedChild> SequencedChildren { get; set; }
}
public class SequencedChild
{
public int Id { get; set; }
public int ParentId { get; set; }
public int ChildId { get; set; }
public int Sequence { get; set; }
}
public class Child
{
public int Id { get; set; }
}
这是我的映射:
HasMany(m => m.SequencedChildren).Inverse().Cascade.Delete();
所以我有一个父级,有一些已排序的子级,我想更新该父级以使其没有子级。当我在父级上没有排序子级的情况下进行更新时,我希望在我的 SequencedChild
表中,具有父级 id 的记录将被删除。但由于某种原因,NHibernate 试图用 null 更新这些记录的 ParentId
- 由于 ParentId
不为 null,因此失败。 编辑:我还希望 Child 对象不受影响(其行为正确)。
我看了几个问题,他们都建议使用逆,但我已经在这样做了。我做错了什么?
I'm having a problem with fluent nhibernate cascade delete. I'm sure I'm doing something wrong because it isn't working.
Here are my objects:
public class Parent
{
public int Id { get; set; }
public IList<SequencedChild> SequencedChildren { get; set; }
}
public class SequencedChild
{
public int Id { get; set; }
public int ParentId { get; set; }
public int ChildId { get; set; }
public int Sequence { get; set; }
}
public class Child
{
public int Id { get; set; }
}
And here is my mapping:
HasMany(m => m.SequencedChildren).Inverse().Cascade.Delete();
So I have a parent with some sequenced children and I want to update that parent to have no children. When I do an update with no sequenced children on that parent I expect that in my SequencedChild
table the records that have the id of the parent will be deleted. But for some reason NHibernate is trying to update the ParentId
of those records with null - which fails as ParentId
is not null. EDIT: I'm also expecting that the Child object is unaffected (which is behaving correctly).
I had a look at a few questions and they all suggest the use of inverse but I am already doing this. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,我设法找到了一个解决方案,结果有多个步骤:
SequencedChild
需要拥有将级联设置为 none 的显式映射Inverse()
调用不是必需的ParentId
字段应该可以为空,因为 nHibernate 坚持将其更新为null 在删除之前。 (如果有人知道解决方法请发表评论)So I managed to find a solution which turned out to have multiple steps:
SequencedChild
needs to have an explicit map that sets cascade to noneInverse()
call is not necessaryParentId
field in the db table should be nullable because nHibernate insists on updating it to null before it deletes it. (if anyone knows a way around this leave a comment)尝试将级联更改为 Cascade.AllDeleteOrphan() 以删除 SequencedChild 表上的孤立子记录。
Try changing the cascade to Cascade.AllDeleteOrphan() to remove orphaned child records on the SequencedChild table.