2

我在M:N关系中有AEntity和BEntity。实体框架 - 从m:n关系中删除M行

在dababase中,我有 ATable, BTable, ABRelationTable。

ABRelationTable建立如下

Id      Guid 
ATableId    Guid 
BTableId    Guid 
AdditionalAttribute1 String 
AdditionalAttribute2 String 

然后我的EntityFramework删除AEntityInstance和
它抛出一个异常,那么它违反了FK在ABRelationTable

如下:

The DELETE statement conflicted with the REFERENCE constraint "FK_AB_A". 
The conflict occurred in database "X", table "ABRelationTable", column 
'Id'.The statement has been terminated. 

如何删除关系表中的相应行

回答

1

必须从A删除关联B的:

foreach(var b in a.Bs.ToList()) 
{ 
    a.Bs.Remove(b); 
} 

或负载a.BsClear()它。

问题是B必须首先加载,然后以某种方式删除,以便更改管理器知道关联被切断。