I have a many-to-one relationship between objects Product
and Supplier
. I need to be able to delete Supplier
without deleting the Product
s that belong to it.
Here is a simplified version of classes:
public class Supplier {
public virtual IList<Product> Products { get; protected set; }
}
public class Product {
// Product belongs to a Category but Supplier is optional
public virtual Supplier Supplier { get; set; }
public virtual Category Category { get; set; }
}
I'm using FluentNHibernate, but here are the mappings it produces:
<bag name="Products" cascade="save-update" inverse="true">
<key column="SupplierID" />
<one-to-many class="Me.Product, Me, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
<many-to-one name="Supplier" column="SupplierID" />
This creates a foreign key on the Products table, so when I attempt to do a straight delete on a Supplier I get a foreign key constraint error. I tried changing cascade to 'all', in the hope that it might only delete the relationship, but it deleted all Products and their other associated objects.
The only way I can see to solve this right now is to iterate the Products collection of Supplier and set the Supplier property to null. Is there a way I can achieve this behaviour through the mapping?