Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all records from a database using LINQ to SQL

I am using MVC. How do I delete all records from the database using LINQ to SQL? A code example would be greatly appreciated.

like image 902
Samir Avatar asked Dec 09 '22 16:12

Samir


1 Answers

If you want to delete all records from a table, you do:

context.Entities.DeleteAllOnSubmit(dc.Entities);

DeleteAllOnSubmit takes an enumerable collection, and while I haven't tried it, it should support this (as dc.Entities is a Table), which should delete all the entities.

You have to do this for all the tables; you cannot just issue one command to do this. Alternatively, you could create a procedure with all of the deletes and just execute that, by adding the proc to the list of methods.

Also, to note, it's faster to drop and recreate the tables in the database than it is to actually perform all those deletes, FYI, if you have a rather large result set in the DB.

like image 86
Brian Mains Avatar answered Jan 27 '23 22:01

Brian Mains