Entity Framework Code First - FOREIGN KEY Constraint Issue

I am new to EF code, the first leader and currently do not know what to do .. I have 2 POCO classes.

public class Problem { public int ProblemID { get; set; } public int UserID { get; set; } public int CategoryID { get; set; } public int RatingID { get; set; } public string Title { get; set; } public string Description { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } public int State { get; set; } public DateTime CreatedOn { get; set; } public virtual Rating Rating { get; set; } public virtual Category Category { get; set; } public virtual User User { get; set; } public virtual ICollection<Picture> Pictures { get; set; } public virtual ICollection<Comment> Comments { get; set; } } 

and

 public class User { public int UserID { get; set; } public int RoleID { get; set; } public string Name { get; set; } public string Surname { get; set; } public string Email { get; set; } public string Password { get; set; } public string SocialHandle { get; set; } public DateTime RegisteredOn { get; set; } public virtual Role Role { get; set; } public virtual ICollection<Point> Points { get; set; } public virtual ICollection<Problem> Problems { get; set; } public virtual ICollection<Comment> Comments { get; set; } } 

My database creation is fine, but when I try to initialize some data in it using a custom initializer, I get the following error:

Entering the FOREIGN KEY 'Problem_User' constraint on the Problems table can cause loops or multiple cascading paths. Indicate ON DELETE NO ACTION or DO NOT UPDATE NO ACTION, or change another FOREIGN KEY of restriction.
Failed to create constraint. See Previous Errors.

This is my initializer:

 protected override void Seed(CleanStreets context) { var adminRole = new Role { Name = "Administrator", RoleID = 0 }; var moderatorRole = new Role { Name = "Moderator", RoleID = 1 }; var userRole = new Role { Name = "User", RoleID = 2 }; context.Roles.Add(adminRole); context.Roles.Add(userRole); context.Roles.Add(moderatorRole); var admin = new User { Name = "admin", Surname = "admin", Role = adminRole, Email = "fake@fake.com", Password = "", RegisteredOn = DateTime.Now, SocialHandle = null }; var user = new User { Name = "user", Surname = "user", Role = userRole, Email = "fake@fake2.com", Password = "", RegisteredOn = DateTime.Now, SocialHandle = null }; context.Users.Add(admin); context.Users.Add(user); var categoryOne = new Category { Title = "Komunalni problemi", CategoryID = 0 }; var categoryTwo = new Category { Title = "Ostali problemi", CategoryID = 1 }; context.Categories.Add(categoryOne); context.Categories.Add(categoryTwo); var problem = new Problem { Category = categoryOne, Title = "Prvi testni problem", Description = "Ovo je testni opis", Latitude = 45.5, Longitude = 15.5, State = 0, CreatedOn = DateTime.Now, User = user }; context.Problems.Add(problem); base.Seed(context); } 

What am I doing wrong? Thank you in advance!

+8
entity-framework foreign-keys ef-code-first cascade
source share
1 answer

It will be because of Comments . EF uses cascading link deletions by default. In your case, the removal of the cascade will be created from User → Problem, User → Comment, as well as from Problem → Comment. If you deleted Cascading a user in the same comment entry, it can be from Problem or from User . This is not allowed in SQL Server. Each entry can only be accessed through one cascading delete path.

To avoid this, you should use free mapping to enable cascading deletion in a single relation (you must choose which one). Example for user → Comment

 public class Context : DbContext { public DbSet<User> Users { get; set; } public DbSet<Comment> Comments { get; set; } public DbSet<Problem> Problems { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<User>() .HasMany(u => u.Comments) .HasRequired(c => c.User) .HasForeignKey(c => c.UserId) .WillCascadeOnDelete(false); base.OnModelCreating(modelBuilder); } } 

There may be other objects and relationships in your model that may cause this problem, but Comment looks obvious from your example.

+15
source share

All Articles