Delete only reference to child object
Posted
by Al
on Stack Overflow
See other posts from Stack Overflow
or by Al
Published on 2010-05-11T01:36:30Z
Indexed on
2010/05/11
1:44 UTC
Read the original article
Hit count: 255
nhibernate
|fluent-nhibernate
I'm running in to a bit of a problem where any attempt to delete just the reference to a child also deletes the child record.
My schema looks like this
Person
Organisation
OrganisationContacts : Person OrgId PersonId Role
When removing an Organisation i want to only delete the record in OrgnaisationContacts, but not touch the Person record.
My Mapping looks like this
Code:
public OrganisationMap()
{
Table("Organsations");
....
HasMany<OrganisationContact>(x => x.Contacts)
.Table("OrganisationContacts ")
.KeyColumn("OrgId")
.Not.Inverse()
.Cascade.AllDeleteOrphan();
}
public class OrganisationContactMap : SubclassMap<OrganisationContact>
{
public OrganisationContactMap()
{
Table("OrganisationContacts");
Map(x => x.Role, "PersonRole");
Map(x => x.IsPrimary);
}
}
At the moment just removing a contact from the IList either doesn't reflect in the database at all, or it issues two delete statements DELETE FROM OrganisationContact & DELETE FROM Person, or tries to set PersonId to null in the OrganisationContacts table. All of which are not desirable.
Any help would be great appreciated.
© Stack Overflow or respective owner