Delete () from collection does not mark object as deleted - EF4

I use POCO generated classes - in any way that I can tease a template so that when removed from a child collection, the deleted item is also removed from db?

Or maybe in a partial class I can redefine something, catch an event ..?

Basically, I want Order.OrderDetails.Remove (orderDetail) to remove orderDetail from db.

I do not want to access the context and make the context .OrderDetails.Delete (orderDetail).

+4
source share
1 answer

When you remove an object from the collection's navigation property, the Entity Framework removes the relationship between the objects (nulling the property of the child object referencing its parent).

If you want to delete a record, you need to mark the object as State = EntityState.Deleted. You can either do this by referring to the context, or if you did not want to, a workaround would be to identify the children who remained orphans in ChangeTracker and set your state there.

var orphans = context.ChangeTracker.Entries().Where(e => e.State == EntityState.Modified && typeof(e.Entity) is ChildType); foreach (DbEntityEntry orphan in orphans) { orphan.State = EntityState.Deleted; } 
+1
source

All Articles