1

I have two table :

TableA :
    Id (identity)
    IdTableB(Foreign Key) Nullable
    ...

TableB :
    Id (identity)
    ...

I did that mapping :

ClassA (tableA)

...
m.ManyToOne<ClassB>(x => x.ClassB, map =>
{
     map.Column("IdTableB");
     map.Cascade(Cascade.All);
     map.Unique(true);
});
...

ClassB (tableB)

...
m.OneToOne<ClassA>(x => x.ClassA, map =>
{
     map.PropertyReference(x => x.ClassB);
     map.Constrained(false);
});   
...

Ok, the mapping is fine and select/save works fine.

Now I want to delete ClassB from a ClassA object, like that :

ClassA classA = session.Load(1);
classA.ClassB = null;
session.commit();

That update de tableA with null on IdTableB, but it do not delete the ClassB from tableB.

First, is it my mapping fine? Is there a better solution?

Second, how can I delete that ClassB object when I explicity delete from relation?

Thanks

Paul
  • 12,359
  • 20
  • 64
  • 101

1 Answers1

1

As tried to explain here: NHibernate Many-to-one cascade,

Many-to-one does not support cascade all-delete-orphan. And this kind of setting is able to delete entity, which is not referenced any more...

Other words, making many-to-one reference null - will not trigger delete.

So, mapping is ok, but functionality required above is not in place. Solution would be to explicitly delete the reference session.Delete(classA.ClassB)...

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335