Cascade remove issues in Entity Framework - Foreign key set to null

c# cascading-deletes entity-framework

Question

I used the Entity Framework to map the following model I have: Member -> Order -> Part Project

Everything in the database is configured using foreign keys and "on delete cascade." Everything is good if I run certain tests on the database. When I create and particularly when I remove objects using the Entity Framework, the issue appears. Take into account this code:

Mitglieder m1 = new Mitglieder();
m1.Name = "erstes";

Auftraege a1 = new Auftraege();
a1.Name = "a1";
m1.Auftraege.Add(a1);

Teilprojekte t1 = new Teilprojekte();
t1.Name = "t1";
a1.Teilprojekte.Add(t1);

context.AddToMitglieder(m1);


Mitglieder m2 = new Mitglieder();
m2.Name = "zweites";

Auftraege a2 = new Auftraege();
a2.Name = "a2";
m2.Auftraege.Add(a2);

Teilprojekte t2 = new Teilprojekte();
t2.Name = "t2";
a2.Teilprojekte.Add(t2);

context.AddToMitglieder(m2);
context.SaveChanges();

This sets the foreign keys and appropriately adds all the items. If I then run the code below to remove the Auftraege:

 var members = context.Mitglieder.ToList();

 var mem1 = members.Single(m => m.Name == "erstes");
 mem1.Auftraege.Load();
 var auf1 = mem1.Auftraege.First();
 context.DeleteObject(auf1);

 var mem2 = members.Single(m => m.Name == "zweites");
 mem2.Auftraege.Load();
 var auf2 = mem2.Auftraege.First();

 //THIS IS THE LINE THAT MAKES THE DIFFERENCE
 auf2.Teilprojekte.Load();

 context.DeleteObject(auf2);

 context.SaveChanges();

The Teilprojekt t1 is appropriately destroyed, together with the two Auftraege a1 and a2, however the Teilprojekt t2 is not erased;

It's field for AuftragID is set to NULL.

The only change between a1 and a2 is that I loaded the Teilprojekte for a2. Why this isn't functioning correctly is beyond my comprehension. Additionally, everything is set up properly in the SSDL:

    <Association Name="FK_Auftraege_Mitglieder">
      <End Role="Mitglieder" Type="TechnBuero.Store.Mitglieder" Multiplicity="0..1">
        <OnDelete Action="Cascade" />
      </End>
      <End Role="Auftraege" Type="TechnBuero.Store.Auftraege" Multiplicity="*" />
      <ReferentialConstraint>
        <Principal Role="Mitglieder">
          <PropertyRef Name="ID" />
        </Principal>
        <Dependent Role="Auftraege">
          <PropertyRef Name="Mitglieder_ID" />
        </Dependent>
      </ReferentialConstraint>
    </Association>
    </Association>
        <Association Name="FK_Teilprojekte_Auftraege">
      <End Role="Auftraege" Type="TechnBuero.Store.Auftraege" Multiplicity="0..1">
        <OnDelete Action="Cascade" />
      </End>
      <End Role="Teilprojekte" Type="TechnBuero.Store.Teilprojekte" Multiplicity="*" />
      <ReferentialConstraint>
        <Principal Role="Auftraege">
          <PropertyRef Name="ID" />
        </Principal>
        <Dependent Role="Teilprojekte">
          <PropertyRef Name="AuftragsID" />
        </Dependent>
      </ReferentialConstraint>
    </Association>

I see the following Entity Framework issue if I change the AuftragsID on the table Teilprojekte to not null:

Error 1 Error 3023: Problem in Mapping Fragments starting at lines 638, 702: Column Teilprojekte.AuftragsID has no default value and is not nullable. A column value is required to store entity data. 
An Entity with Key (PK) will not round-trip when:
((PK is in 'Teilprojekte' EntitySet OR PK plays Role 'Teilprojekte' in AssociationSet 'FK_Teilprojekte_Auftraege') AND (PK is NOT in 'Teilprojekte' EntitySet OR PK does NOT play Role 'Teilprojekte' in AssociationSet 'FK_Teilprojekte_Auftraege'))

Could someone please explain to me what's wrong and how to fix this? Thanks

1
2
3/31/2010 7:25:16 AM

Popular Answer

Only loaded (connected) entities will cascade when using the EDMX's cascade delete. So, if your object is not loaded, the delete will not cascade. Your database should cascade the deletion, according to EF.

  1. Make sure your database is configured with a cascade delete.
  2. Cascade delete should be set in your EDMX.
  3. Make careful to load all entities before removing if you can't accomplish step one.
2
4/2/2010 11:46:04 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow