I\'m trying to perform a DELETE using LINQ that will generate a single query.
Here\'s how I\'m doing it:
// Northw
One way to have a batch delete is setting ON CASCADE DELETE on foreign keys. You need to set CASCADE on relation in designer and Set ON CASCADE DELETE on relation in database.
To delete Products navigation property from Order EF will do (number of Products properties)+1 statements to database.
I prefer to do it:
var order = context.Orders.Include(p=>p.Products).Where(o=>o.Order_ID == 11076);
foreach(Product product in order.Products.ToList())
{
context.Entry(product).State = EntityState.Deleted;
}
context.Entry(order ).State = EntityState.Deleted;
context.SaveChanges();
Hope it helps.
Install Entity Framework Extended Library (PM> Install-Package EntityFramework.Extended)
Import EntityFramework.Extensions in your code
Delete records specified by inner query
context.Orders.Where(o=>o.User_ID == 1).Delete();
Deletes all records inside Orders with userID = 1