Benutzer mit den meisten Antworten
Frage zum Entity Framework WillCascadeOnDelete

Frage
-
Hallo NG,
ich habe 2 Tabellen:
Employee hat eine List von InformationsTypes
InformationsTypes hat einen Key auf die Tabelle Employee
HasRequired<Employees>(s => s.Employee)
.WithMany(s => s.InformationsTypesList)
.HasForeignKey(s => s.EmployeeRid)
.WillCascadeOnDelete(true);HasMany(e => e.InformationsTypesList)
.WithRequired(e => e.Employee)
.HasForeignKey(e => e.EmployeeRid)
.WillCascadeOnDelete(true);So wie ich es verstehe, kann ich den Employee löschen, dann wird die Abhängigkeit InformationsTypes ebenfalls gelöscht. Umgekehrt auch. Ich möchte aber das der Employee nicht gelöscht werden kann, solange noch InformationsTypes vorhanden sind. Wenn ich aber beim Employee den WillCascadeOnDelete(false) mache bekomme ich eine Fehlermeldung von EF.
Wie kann ich das lösen?Danke im voraus für jeden Hinweis und Tipp.
Grüße Ingo
Antworten
-
Hallo Ingo,
dann verwende WithRequiredPrincipal (bei Employees) nach HasRequired.
Mehr siehe z. B.: Entity Framework Fluent API - Relationships, da steht u. a. auch wie WillCascadeOnDelete per Konvention implementiert ist (Null vs. Not Null).
Gruß Elmar
- Als Antwort vorgeschlagen Dimitar DenkovMicrosoft contingent staff, Administrator Freitag, 10. Februar 2017 12:03
- Als Antwort markiert IngoManthey Freitag, 10. Februar 2017 13:40
Alle Antworten
-
Hallo Ingo,
dann verwende WithRequiredPrincipal (bei Employees) nach HasRequired.
Mehr siehe z. B.: Entity Framework Fluent API - Relationships, da steht u. a. auch wie WillCascadeOnDelete per Konvention implementiert ist (Null vs. Not Null).
Gruß Elmar
- Als Antwort vorgeschlagen Dimitar DenkovMicrosoft contingent staff, Administrator Freitag, 10. Februar 2017 12:03
- Als Antwort markiert IngoManthey Freitag, 10. Februar 2017 13:40