Search code examples
c#entity-frameworkentity-framework-4entity-relationship

Entity framework - deleting M row from an m:n relationship


I have AEntity and BEntity in M:N Relationship.

In the dababase I have ATable, BTable, ABRelationTable.

ABRelationTable is built as follows

Id                      Guid
ATableId                Guid
BTableId                Guid
AdditionalAttribute1    String
AdditionalAttribute2    String

I then delete AEntityInstance in EntityFramework and
It throws an exception then that it violates the FK in ABRelationTable

as follows:

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.

How do I delete the corresponding lines in the relation table


Solution

  • Is the relationship has cascade on delete in SQL database table? If so you can do this in EF and it will be taken care of. see... Entity framework - remove related records by id