How to delete a record with a foreign key constraint?

前端 未结 4 509
小蘑菇
小蘑菇 2020-12-19 02:48

Started a new ASP.NET MVC 3 application and getting the following error:

The primary key value cannot be deleted because references to this key stil

4条回答
  •  余生分开走
    2020-12-19 03:07

    Found the solution:

    public class FoodJournalEntities : DbContext
    {
        public DbSet Journals { get; set; }
        public DbSet JournalEntries { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity()
                   .HasOptional(j => j.JournalEntries)
                   .WithMany()
                   .WillCascadeOnDelete(true);
            base.OnModelCreating(modelBuilder);
        }
    }
    

    Source

提交回复
热议问题