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
As tried to explain here: NHibernate Many-to-one cascade,
Many-to-one
does not support cascadeall-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)
...