Entity Framework: how to solve “FOREIGN KEY constraint may cause cycles or multiple cascade paths”?

You can use the fluent api to specify the actions the error message suggests.

In your Context:

protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<aspnet_UsersInRoles>().HasMany(i => i.Users).WithRequired().WillCascadeOnDelete(false);
}

Note that you have not included the definition for the table aspnet_UsersInRoles so this code may not work.

Another option is to remove all CASCADE DELETES by adding this

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

If you need more info about configuring relationships with the fluent api I suggest http://msdn.microsoft.com/en-US/data/jj591620

Leave a Comment