How to delete a record with a foreign key constraint?

Launched a new ASP.NET MVC 3 application and received the following error:

The primary key value cannot be deleted because references to this key still exist.

How to solve this?

Models (EF code first)

public class Journal { public int JournalId { get; set; } public string Name { get; set; } public virtual List<JournalEntry> JournalEntries { get; set; } } public class JournalEntry { public int JournalEntryId { get; set; } public int JournalId { get; set; } public string Text { get; set; } } 

controller

 // // POST: /Journal/Delete/5 [HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(int id) { Journal journal = db.Journals.Find(id); db.Journals.Remove(journal); db.SaveChanges(); // **exception occurs here** return RedirectToAction("Index"); } 

Database setup

 public class FoodJournalEntities : DbContext { public DbSet<Journal> Journals { get; set; } public DbSet<JournalEntry> JournalEntries { get; set; } } 
+7
source share
3 answers

Found a 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); } } 

A source

+17
source

If you delete an entry from a table (say, "blah") that has other relationships with other tables (xyz, abc). By default, the database will prevent the row from being deleted in "blah" if one of the other tables has related rows.
Solution No. 1:
At first you can manually delete the related lines, this may take a lot of work.
Solution No. 2:
a simple solution is to configure the database to automatically delete them when you delete the string "blah".

Follow this, open the database diagram and click on the relationship properties

enter image description here

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

Save and close the chart. If you are asked if you want to update the database, click Yes.

To make sure that the model keeps the entities in memory in synchronization with what the database does, you must establish the appropriate rules in the data model. Open SchoolModel.edmx, right-click the association string between "blah" and "xyz," and then select "Properties."

In the Properties window, expand INSERT and UPDATE 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

+8
source

I found him...

Migrate to SQL Server

Make your database diagram

Right-click on the relationship line between the parent and child and open its property.

Install INSERT and refine the specification and just install DELETE RULE TO CASCADE.

Remember that the project does not require code for this APPOINTMENT, and just debug and enjoy it.

-1
source

Source: https://habr.com/ru/post/911001/


All Articles