且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Edmx Designer中启用级联删除多对多关系

更新时间:2022-11-05 10:05:24

似乎是一个edmx限制,真的不明白代码优先完全能够生成具有两个级联ON DELETE约束的连接表,但是首先和首先在数据库中的连接表在edmx中不允许相同的配置。通常,级联动作在关联的一端配置。也许是太复杂了,检查' * '结束时级联动作的有效性(只有当两端都是' * ')。

It seems to be an edmx restriction, which I don't really understand. Code-first is perfectly capable of generating a junction table with two cascading ON DELETE constraints, but model-first and database-first do not allow the same configuration in the edmx. Normally, cascade actions are configured on the 'one' end of an association. Maybe it is too complex to check the validity of cascade actions on '*' ends (only when both ends are '*').

对于基于edmx模型的上下文发生的级联删除,您必须加载父级其子级然后删除父项。

For the cascaded delete to happen with a context based on an edmx model, you have to load a parent and its children and then delete the parent.

var cls = db.Classes.Include(c => c.Students).Single(c => c.Id = 1);
db.Classes.Remove(cls);
db.SaveChanges();

执行的SQL语句显示 Class JOIN 语句中从学生中的数据库中获取。然后分别删除 StudentClass Class

The executed SQL statements show that the Class is fetched from the database in a JOIN statement with Student. Then the StudentClasss and the Class are deleted respectively.

显然,这比在数据库中启用级联删除要贵得多。

Obviously, this is much more expensive than enabling cascaded delete in the database.

每次修改DDL的方法都不是有吸引力的,当然。但我认为唯一的选择是使模型的一部分 StudentClass ,并在新关联的一端配置级联删除。或者先去代码优先。

The work-around to modify the DDL each time after is was generated is not attractive, of course. But I think the only alternative is to make StudentClass part of the model and configure cascaded delete on the 'one' ends of the new associations. Or go code-first.