Reputation: 38910
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?
Upvotes: 1
Views: 5389
Reputation: 38910
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)Upvotes: 3
Reputation: 11661
Try changing the cascade to Cascade.AllDeleteOrphan() to remove orphaned child records on the SequencedChild table.
Upvotes: 1