【发布时间】:2009-04-29 10:13:32
【问题描述】:
对象Product 和Supplier 之间存在多对一关系。我需要能够在不删除属于它的Products 的情况下删除Supplier。
这是类的简化版本:
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 表上创建一个外键,因此当我尝试对供应商执行直接删除时,我会收到外键约束错误。我尝试将级联更改为“全部”,希望它可能只会删除关系,但它删除了所有产品及其其他关联对象。
我现在可以看到解决此问题的唯一方法是迭代 Supplier 的 Products 集合并将 Supplier 属性设置为 null。有没有办法通过映射实现这种行为?
【问题讨论】:
-
数据库是自动生成的,所以没有。
-
此外,我会有没有关系的虚拟供应商ID 值
标签: nhibernate nhibernate-mapping many-to-one