级联删除使用单向多对多映射 [英] Cascade on delete using unidirectional Many-To-Many mapping

查看:207
本文介绍了级联删除使用单向多对多映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Fluent和NHibernate。
我有两个对象说A& B之间有多对多的关系。
当HasMany B使用单向多对多映射时。在B中没有关于A(单向)的引用。

这在数据库中创建了第三个表(名为ABMapping),它具有与A的主键相关的两列&安培; B。



如果删除对象A,则删除与AB相关的ABMapping表中的条目。这很酷。

但是,现在我不能删除一个对象B,因为它有一个FK约束。我该如何设置它,以便在删除B时,自动删除AB映射中与B有关的所有条目?如果B不引用A,那么它不知道映射表,所以它不能级联删除。正如我所看到的,您有两种选择:


  1. 使用FK或触发器上的级联删除级联数据库中的删除。
  2. >
  3. 将关系从B映射到A;您不必将其暴露给您的班级的消费者,则可以使用访问策略将A集合映射为专用字段。我总是这样做的集合(使用.Access.CamelCaseField(前缀.Underscore)),所以我不公开IList。


I am using Fluent and NHibernate. I have two objects say A & B which has a many-to-many relationship between them. I am using a unidirectional many-to-many mapping when A HasMany B's. There is no reference in B about A (Unidirectional).

This creates a third table (named ABMapping) in the Database which has the two columns relating to primary keys of A & B.

If I delete the object A, the entries from the ABMapping table related to A are deleted. That's cool.

But, now I am not able to delete an object B, as it has a FK constraint. How can I set it up so that on deleting B, all entries related to B in ABMapping are deleted automatically?

解决方案

If B doesn't reference A then it doesn't know about the mapping table so it can't cascade the delete. As I see it you have two options:

  1. Cascade the delete in the database using cascading deletes on your FK or a trigger.
  2. Map the relationship from B to A; you don't have to expose it to consumers of your class, the A collection could be mapped as a private field using an access strategy. I always do this for collections (using .Access.CamelCaseField(Prefix.Underscore)) so that I don't expose IList.

这篇关于级联删除使用单向多对多映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆