Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Entity Framework and the error The DELETE statement conflicted with the REFERENCE constraint.

Posted on: 2013-02-19

Entity Framework can raise an error concerning a conflict with reference when deleting an entity. One of this error is the following one.

The DELETE statement conflicted with the REFERENCE constraint.

To solve this issue we need to delete cascade instead of a simple remove. We have seen in a previous post how to use delete in cascade with Entity Framework.

In short, you have to specify the many side to the other side (required or optional) and then specify the delete cascade statement with the true parameter.

 ...HasMany(e => e.ParentDetails) 
 .WithOptional(s => s.Parent) 
 .WillCascadeOnDelete(true);