1

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?

Darko
  • 38,310
  • 15
  • 80
  • 107

2 Answers2

3

So I managed to find a solution which turned out to have multiple steps:

  1. As James pointed out in a comment ParentId/ChildId should be Parent/Child references not just the ids (+1 for that)
  2. SequencedChild needs to have an explicit map that sets cascade to none
  3. When doing the update, don't overwrite the SequencedChild list. First clear it then add the new items. (Or just clear if you arent replacing the items)
  4. The Inverse() call is not necessary
  5. The ParentId 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)
Darko
  • 38,310
  • 15
  • 80
  • 107
  • 1
    Hi, have you tried marking the References to the Parent with Not.Nullable()? I have a similar scenario to yours where I have added Not.Nullable() to the childs References and this creates the column as NOT NULL. – Joe Feb 23 '11 at 00:25
  • Thanks for this heads up, I also could not seem to get around the necessity of making my foreign key column nullable, thanks for helping me figure out where to work. @Joe, while that may create the column as not null, it still fails at runtime because nhibernate updates the row to have a null id before it deletes. – kirps Sep 07 '11 at 05:30
  • Can you please take a look at my question http://stackoverflow.com/questions/22380027/flunet-nhibernate-mapping-cascade , I can not get it to work , looks like I have same problem... – Nic Mar 13 '14 at 15:05
1

Try changing the cascade to Cascade.AllDeleteOrphan() to remove orphaned child records on the SequencedChild table.

James Kovacs
  • 11,549
  • 40
  • 44
  • The problem is that orphans can't be created since ParentId is NotNull and NHibernate is trying to set them to null. – Darko Dec 03 '10 at 01:14
  • Does your SequencedChild class really have ParentId/ChildId rather than references to the Parent/Child? If so, Inverse() isn't going to work as you're telling NHibernate that the Child is managing the one-to-many relationship, but the child doesn't have a back-reference to its parent. There is no way for NHibernate to tell that ParentId (an int) is the back-reference. – James Kovacs Dec 03 '10 at 01:39