Entity Framework 4 Delete Object from entity collection

The answer depends on the way you modeled your entities. If you are using common independent relation or foreign key relation you will have to use your current approach – I’m using it as well in my project.

If you defined identifying relation you will be able to call just Clear on collection as @Craig described. Identifying relation is special relation where primary key of dependent entity contains foreign key of parent entity.

Example EF model

The example shows Order entity and OrderItem entity with foreign key identifying relation between them. Primary key of OrderItem consists of unique Id and OrderId which is FK of Order table. With this configuration you don’t need to iterate through OrderItems and delete each item separately. Simply removing OrderItem from collection will be executed as delete in database and clearing collection will delete all related OrderItems in database.

Leave a Comment