Saturday, December 26, 2015

MVC 5 Code First ApplicationDbContext throws error

In my MVC 5 Code First project I decided to put my domain tables under the out of box ApplicationDbContext


When I run the command "update-database ..." I got this error:
"Introducing FOREIGN KEY constraint 'FK_dbo.aaa_dbo.bbb_abcId' on table 'aaa' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors."

This is because I had not null 2 foreign keys in same table that both reference the primary key in same parent table. This creates 2 conflicting cascade delete paths. The solution is to make one of them nullable. See the source link:

No comments: