How to delete a record with a foreign key constraint?

前端 未结 4 492
小蘑菇
小蘑菇 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 02:52

    If you delete a record from a table(lets say "blah"), which has other relationships with other tables (xyz,abc). By default, the database will prevent you from deleting a row in "blah" if there are related rows in one of the other tables.
    Solution #1:
    You can manually delete the related rows first,this may require a lot of work.
    Solution #2:
    an easy solution is to configure the database to delete them automatically when you delete a "blah" row.

    Follow this open your Database diagram,and click on the properties on the relationship

    enter image description here

    In the Properties window, expand INSERT and UPDATE Specification and set the DeleteRule property to Cascade.
    enter image description here

    Save and close the diagram. If you're asked whether you want to update the database, click Yes.

    To make sure that the model keeps entities that are in memory in sync with what the database is doing, you must set corresponding rules in the data model. Open SchoolModel.edmx, right-click the association line between "blah" and "xyz", and then select Properties.

    In the Properties window, expand INSERT and UPDATE Specification and set the DeleteRule property to Cascade.

    Solution and images taken from http://www.asp.net/web-forms/tutorials/getting-started-with-ef/the-entity-framework-and-aspnet-getting-started-part-2

    0 讨论(0)
  • 2020-12-19 03:07

    Found the solution:

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

    Source

    0 讨论(0)
  • 2020-12-19 03:15

    In EF Core (3.1.8), the syntax is a bit different than the accepted answer but the same general idea, what worked for me is below:

    modelBuilder.Entity<Journal>()
                .HasMany(b => b.JournalEntries)
                .WithOne()
                .OnDelete(DeleteBehavior.Cascade);
    

    In your query to select the item to delete or remove from the database you want to make sure that you are explicitly including the items as well, otherwise it will continue to throw a FK error, something like below.

    var item = _dbContext.Journal.Include(x => x.JournalEntries).SingleOrDefault(x => x.Id == id);
    
    0 讨论(0)
  • 2020-12-19 03:16

    I found it...

    Go TO SQL Server

    Make his Database diagrammed

    Right click on relation ship line between parent and child and open the property of it.

    Set INSERT And Update Specification and simply set DELETE RULE TO CASCADE.

    Remember No code is required in Project FOR this PURPOSE and simply debug and enjoy it.

    0 讨论(0)
提交回复
热议问题