rozpoczął nową aplikację ASP.NET MVC 3 i otrzymuję następujący błąd:Jak usunąć rekord za pomocą ograniczenia klucza obcego?
The primary key value cannot be deleted because references to this key still exist.
Jak rozwiązać ten problem?
modele (code-first EF)
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; }
}
Ustaw.regulatora
//
// 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");
}
DB
public class FoodJournalEntities : DbContext
{
public DbSet<Journal> Journals { get; set; }
public DbSet<JournalEntry> JournalEntries { get; set; }
}
W bazie danych można wybrać opcję kaskadowego usuwania dla tabeli nadrzędnej? – Pankaj
Twoja odpowiedź może już być [tutaj] (http://stackoverflow.com/a/5522422/60761). Jeśli nie, opublikuj kod DbSetup. Możesz usunąć widok. –
@PankajGarg - zweryfikowałem, że baza danych ma już ten zestaw. –