NHibernate级联删除
我在从 A 到 B 的数据模型中具有一对多关系。但是在我的域 API 中,我不会在 A 上公开“B”(因为我们永远不会从 A 导航到 B),但有来自的引用B到A。现在我希望能够在删除A时删除所有“B”。是否可以?现在 NH 首先尝试将 FK 设置为 null,这是我不想要的,并且不能,因为列不可为空。
A = 供应商类型
B = 基础产品共同保险
public BaseProductCoInsuranceMap()
{
Table("BaseProductCoInsurance");
Id(x => x.Id, "BaseProductCoInsuranceId");
Map(x => x.CoInsurancePercent).Column("CoInsrPrcnt");
References(x => x.BaseProduct, "BaseProductId");
References(x => x.PolicySupplierType, "PlcySupplierTypeID");
References(x => x.InsuredType, "InsuredTypeCode");
}
I have a one-to-many relationship in data model from A to B. But in my domain API I do not expose "B"s on A (since we will never navigate from from A to B), but have a reference from B to A. Now I want to be able to delete all "B"s when A is deleted. Is it possible? Right now NH is trying first to set FK to null, which I don't want, and cannot since column is not nullable.
A = SupplierType
B = BaseProductCoInsurance
public BaseProductCoInsuranceMap()
{
Table("BaseProductCoInsurance");
Id(x => x.Id, "BaseProductCoInsuranceId");
Map(x => x.CoInsurancePercent).Column("CoInsrPrcnt");
References(x => x.BaseProduct, "BaseProductId");
References(x => x.PolicySupplierType, "PlcySupplierTypeID");
References(x => x.InsuredType, "InsuredTypeCode");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要能够级联删除,那么您需要让 NHibernate 了解这种关系。也就是说,您不需要让其他人可以接触到这种关系。您当然可以拥有只有 NH 知道的私人收藏。
如果您将关系设置为延迟加载,您甚至不会看到性能受到影响。
另一个需要考虑的选择是修改您的删除方法以同时删除其他实体。
If you need to be able to cascade delete then you need to let NHibernate know about the relationship. That said you don't need to make the relationship accessible to others. You could of course have a private collection that only NH knows about.
If you make the relationship lazy loaded you shouldn't even see a performance hit from this.
Another option to consider is to just modify your delete method to also delete the other entity.