Question

I am trying to delete one of my entities but I am receiving a strange error along it.

My code to delete one looks like this:

public bool Delete()
{
    using (var context = new DbContext())
    {
        context.Set(this.GetType()).Attach(this);
        context.Entry(this).State = EntityState.Deleted;

        context.SaveChanges();
    }
}

This method is part of my Entities and it attaches itself (the current entity) and changes the State to deleted.

On the line context.SaveChanges(); I am receiving the error message:

Translated Message (my own translation):

The execution is aborted: The relation could not be changed, because for atleast one foreignkey property no NULL-value is allowed. When a relation is modified, the related foreignkey property is set on a NULL-value. If the foreignkey doesnt support NULL-value, you have to specify a new relation or set the foreignkeyproperty to a different NON-NULL-value or the non related object get deleted.

Original Message:

Der Vorgang ist fehlgeschlagen: Die Beziehung konnte nicht geändert werden, da für mindestens eine der Fremdschlüsseleigenschaften keine NULL-Werte zulässig sind. Wenn eine Beziehung geändert wird, wird die verwandte Fremdschlüsseleigenschaft auf einen NULL-Wert festgelegt. Wenn der Fremdschlüssel keine NULL-Werte unterstützt, muss eine neue Beziehung definiert, die Fremdschlüsseleigenschaft einem anderen Nicht-NULL-Wert zugeordnet oder das nicht verwandte Objekt gelöscht werden.

When I attach my object the relations seems to be intakt, once I change the state to Deleted the NavigationProperties get set to NULL, but why would EF try to change relations when I desire just a simple delete command?

Edit:

As recommended in the comments below. I have set the Association between my Tables to have Cascade delete on, but for some reason it gets ignored.

Thats how my association looks like:

End1 Multiplicity - 1 (One of TabAdjust)
End1 Navigation Property - TabAdjustAccounts
End1 OnDelete - Cascade
End1 Role Name - TabAdjsut

End2 Multiplicity - * (collection of TabAdjustAccount)
End2 Navigation Property - TabAdjust
End2 OnDelete - None
End2 Role Name - TabAdjustAccount
Name - CS_TABADJUSTTABADJUSTACCOUNT
Referential Contraint - TabAdjust -> TabAdjustAccount

Its a 1:N relation between TabAdjust and TabAdjustAccount.

In my example I am trying to delete TabAdjust, the TabAdjustAccounts Navigation Property gets cleared once I change the state to deleted before that line the TabAdjustAccounts Property has items in it.

Edit2:

I am using DataBase first approach.

Edit3:

See my answer below maybe you can explain why this change makes a difference.

Was it helpful?

Solution 3

Dont know if its a bug or feature but when I change my code from this:

public bool Delete()
{
    using (var context = new DbContext())
    {
        context.Set(this.GetType()).Attach(this);
        context.Entry(this).State = EntityState.Deleted;

        context.SaveChanges();
    }
}

To this:

public bool Delete()
{
    using (var context = new DbContext())
    {
        context.Set(this.GetType()).Attach(this);
        context.Set(this.GetType()).Remove(this); // <-- changed line...

        context.SaveChanges();
    }
}

everything is working fine and the cascade delete kicks in successfully.

I wont accept this as an answer because maybe someone knows why.

OTHER TIPS

In my own project this problem was oracle related. In Oracle.ManagedDataAccess 4.1.112.1 the call via "context.Entry(this).State = EntityState.Deleted;" worked fine.

After an update to 4.121.2.0 same problem occured, the cascading deletes doesnt fired anymore.

Your approach with "context.Set(this.GetType()).Remove(this);" worked for me too. But Oracles correct way for inserting and deleting objects is:

public bool Delete()
{
    using (var context = new DbContext())
    {
        context.MyEntityType.Remove(this); 
        //context.DeleteObject(this); <- in older versioned context

        context.SaveChanges();
    }
}

Oracle Docu: inserting and deleting

A quick search for "entity framework ignore cascade delete" found this answer to another SO question. You may need to enable the cascading delete on the dependent table:

.WillCascadeOnDelete(true);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top