Update and Delete
A current limitation of the Entity Framework is that in order to update or delete an entity you have to first retrieve it into memory. Also, for single deletes, the object must be retrieved before it can be deleted requiring two calls to the database. To overcome this problem we have to extend the current entity framework using EntityFramework.Extended. EntityFramework.Extended have useful features like Batch Update and Delete, Audit log, Query Result cache, Future Queries. Batch update and delete eliminates the need to retrieve and load an entity before modifying it. Here are a few lines of code to demonstrate how to delete, update.
Install via nuget
PM> Install-Package EntityFramework.Extended
Update
Scenario: Update customers which have country USA.
If we do this without any extensions, we have to fetch all customers which have country USA, modify the list and update it using loops. Using Entity Framework.Exdended we don’t need to fetch the list of customers, simply add where condition, set update data and execute query.
static void Main(string[] args)
{
using(var db = new DataContext())
{
db.Customers.Where(c => c.Country == "USA").Update(c => new Customer()
{
Country = "IN"
});
foreach(var customer in db.Customers.ToList())
{
Console.WriteLine("CustomerInfo - {0}-{1}-{2}", customer.Name, customer.Country, customer.Status);
}
}
Console.ReadLine();
}
https://code.msdn.microsoft.com/entity-framework-batch-994cd739