To delete all the rows in a table, I am currently doing the following:
context.Entities.DeleteAllOnSubmit(context.Entities);
context.SubmitChanges();
However, this seems to be taking ages. Is there a faster way?
To delete all the rows in a table, I am currently doing the following:
context.Entities.DeleteAllOnSubmit(context.Entities);
context.SubmitChanges();
However, this seems to be taking ages. Is there a faster way?
You could do a normal SQL truncate or delete command, using the DataContext.ExecuteCommand method:
Or
The way you are deleting is taking long because Linq to SQL generates a DELETE statement for each entity, there are other type-safe approaches to do batch deletes/updates, check the following articles:
Unfortunately LINQ-to-SQL doesn't execute set based queries very well.
You would assume that
will translate to something like
but unfortunately it's more like
You'll find the same when you try to do bulk update in LINQ-to-SQL. Any more than a few hundred rows at a time and it's simply going to be too slow.
If you need to do batch operations & you're using LINQ-to-SQL, you need to write stored procedures.
I like using an Extension Method, per the following:
The Below c# code is used to Insert/Update/Delete/DeleteAll on a database table using LINQ to SQL