Entity Framework DbContext .Remove(obj) vs .Entry(obj).State = EntityState.Deleted

c# entity-framework

Question

I'm starting with EF Code. a basic example

  item { public int Id {set; get;},... ,ICollection<ItemImages> {set; get;} }

    itemImages { 
         public int Id {set; get; },
         public int ItemId {set; get; }
          , ... ,
         public Item Item  {set; get; }
      }

ItemConfig:EntityTypeConfiguration<Item>
{
 //some config statement;
 //...
// mark child delete when parent delete: waterfall delete.
 HasRequired(rs => rs.ItemCat).WithMany(rs => rs.Items).HasForeignKey(rs => rs.ItemCatId).WillCascadeOnDelete(true);
}

When an entity is deleted with the code Remove(), all linked children (records of item images) are also deleted.

_db.Item.Remove(DeleteThisObj);
_db.SaveChanges();

however, if you delete it:

_db.Entry(DeleteThisObj).State = EntityState.Deleted;
_db.SaveChanges();

find error

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

1
7
7/15/2014 12:59:49 PM

Popular Answer

You would need to make your foreign keys nullable if you truly wanted to use Deleted, but it would result in orphaned records, which is one of the main reasons you shouldn't be doing that in the first place. So simply utilizeRemove()

The entity is designated as Deleted in the context by ObjectContext.DeleteObject(entity). After that, its EntityState is deleted. The database receives a SQL DELETE statement from EF if you later run SaveChanges. The entity will be destroyed if no referential restrictions in the database are broken; otherwise, an exception is raised.

The relationship between the parent and childEntity is marked as Deleted by EntityCollection.Remove(childEntity). Depending on the nature of the relationship between the two, what precisely occurs when you use SaveChanges and whether the childEntity itself is removed from the database:

If the connection is optional, and the database's foreign key that connects the child to the parent accepts NULL values, it will be set to null, and if you execute SaveChanges, the database will record this NULL value for the childEntity (i.e. the relationship between the two is removed). A SQL UPDATE statement causes this. DELETE command is not executed.

You must either add the child to another parent or explicitly delete the child if the relationship is necessary (the FK doesn't permit NULL values) and not identifying (the foreign key is not a component of the child's (composite) primary key) (with DeleteObject then). When a referential constraint is broken, EF will issue the infamous "The relationship could not be modified because one or more of the foreign-key properties is non-nullable" exception or anything similar when you run SaveChanges.

If the relationship is identifiable, which it must be because the main key cannot include any NULL characters, EF will also classify the childEntity as Deleted. A SQL DELETE statement will be issued to the database if you use SaveChanges. The entity will be removed if no other referential restrictions in the database are broken; otherwise, an exception is raised.

It's important to note that setting..State = EntityState.Deleted does not cause a change to be automatically noticed.

14
7/17/2014 10:09:54 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