我在对象Product
和Supplier
. 我需要能够在Supplier
不删除Product
属于它的 s 的情况下删除。
这是类的简化版本:
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; }
}
我正在使用 FluentNHibernate,但这里是它产生的映射:
<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" />
这会在 Products 表上创建一个外键,因此当我尝试对供应商进行直接删除时,我会收到外键约束错误。我尝试将级联更改为“全部”,希望它可能只删除关系,但它删除了所有产品及其其他关联对象。
我现在能看到解决这个问题的唯一方法是迭代供应商的产品集合并将供应商属性设置为空。有没有办法通过映射实现这种行为?