how to delete all records from database using linq.i want to delete all records from the database using linq.i am using MVC.so please tell me the code to delete all records using repository.
reply me soon
thanks samir
how to delete all records from database using linq.i want to delete all records from the database using linq.i am using MVC.so please tell me the code to delete all records using repository.
reply me soon
thanks samir
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.
Dear Samir,
check these links
http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx
you cannot use linq for that since linq is just a query language and not a data manipulation language. use a dataadapter or createstatement or sql for this task instead.
Considering you're referring to Linq to SQL, I think it is not possible to do what you want (clear all database entities) with just one command line.
You could use SQL truncate
or delete
command to delete each one of your entities, using DataContext.ExecuteCommand
, as follows:
context.ExecuteCommand("DELETE FROM EntityName");
Or
context.ExecuteCommand("TRUNCATE TABLE EntityName");
Although, you still have to do this for each one of your entities and pay attention to relationships between entities (foreign keys).
Other references: