Entity framework provides numerous ways to batch delete the objects from Database. Below are the step for way 1 :
- Load objects that has to be deleted in memory.
Remove the objects from context
- And then save these changes. This statement will reflect the actual database changes.
var users = context.Users.Where(u => u.Name == "test");
foreach (var u in users)
{
context.Users.Remove(u);
}
context.SaveChanges();
second way for batch delete :
context.Database.SqlQuery<User>(
"DELETE FROM Users WHERE Name = @name",
new [] { new SqlParameter("@name", "John") }
);
0 Comment(s)