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.
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 OrderItem
s and delete each item separately. Simply removing OrderItem
from collection will be executed as delete in database and clearing collection will delete all related OrderItem
s in database.
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.
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 OrderItem
s and delete each item separately. Simply removing OrderItem
from collection will be executed as delete in database and clearing collection will delete all related OrderItem
s in database.
Remove item from collection entity framework
This is very dependent upon the configuration of the relationships. See Entity Framework .Remove() vs. .DeleteObject()
Since your relationship sounds to be a many to many, you will likely need to call DeleteObject for the addresses themselves, as EF won't automatically delete the orphaned records.
How to remove a subset of items from an Entity Framework Collection
I created a seperate list and it seems to have worked:
// Perform the deletes
var reqsToDelete = order.Requirements.Where(x=>!orderContract.Requirements.Any(y=>y.RequirementId==x.RequirementId)).ToList();
foreach (var deleteReq in reqsToDelete)
{
order.Requirements.Remove(deleteReq);
}
That way, when I remove the item from the order.Requirements list, it is not affecting the list that I am iterating.
Entity Framework Core, deleting items from nested collection
That is because the rows in the database are not marked for deletion.
Only new or changed items are updated. 'Missing' items from a collection are not considered to be deleted.
So what you'll need to do is mark the items for deletion yourself. Something like this:
public void Update(Invoice record)
{
var missingRows = dB.InvoiceRows.Where(i => i.InvoiceId == record.Id)
.Except(record.Rows);
dB.InvoiceRows.RemoveRange(missingRows);
dB.Invoices.Update(record);
dB.SaveChanges();
}
EF 4: Removing child object from collection does not delete it - why?
You aren't deleting the object with the remove statement. Instead you are attempting to alter a record and make it an orphan (by setting the foreign key to null). The database has a non-null constraint on that column and prevents you from doing so.
EF code first: How to delete a row from an entity's Collection while following DDD?
I've solved this problem in the application I'm currently working on by using domain events; a DDD concept Eric Evans said should have been in his book.
While domain objects aren't allowed to know about the object context, an IDomainEventHandler
is - I've therefore got a DomainObjectDeletionHandler
which deletes 'removed' objects from the object context before control returns to my application layer and the changes are saved.
For more information, I've written a blog about my implementation of domain events and how I approached hooking everything together.
Hope that helps :)
Edit
For example, if you have an Order
class which has an OrderItems
collection of type OrderItem
:
public class Order
{
// Other stuff
public void RemoveOrderItem(int orderItemId)
{
var orderItemToRemove = OrderItems.First(oi => oi.Id == orderItemId)
OrderItems.Remove(orderItemToRemove);
DomainEvents.Raise(new OrderItemRemoved(orderItemToRemove));
}
}
Related Topics
How to Combine More Than Two Generic Lists in C# Zip
How to Fix the Microsoft Visual Studio Error: "Package Did Not Load Correctly"
Servicestack.Net Redis: Storing Related Objects VS. Related Object Ids
Custom Collection Initializers
Assembly Binding Redirect: How and Why
ASP.NET Core: Exclude or Include Files on Publish
Dotnet Core System.Text.JSON Unescape Unicode String
Convert a Binary String Representation to a Byte Array
Dynamically Adding Properties to an Expandoobject
An Attempt Was Made to Access a Socket in a Way Forbidden by Its Access Permissions. Why
How to Delete Cookies from Windows.Form
Creating Instance of Type Without Default Constructor in C# Using Reflection
Listen to Changes of Dependency Property
How to Retrieve Data Annotations from Code? (Programmatically)