Entity Framework. Delete all rows in table

Zhenia picture Zhenia · Mar 5, 2013 · Viewed 257.9k times · Source

How I can quickly remove all rows in table using Entity Framework?

I am currently using:

var rows = from o in dataDb.Table
           select o;
foreach (var row in rows)
{
    dataDb.Table.Remove(row);
}
dataDb.SaveChanges();

However, it takes a long time to execute.

Are there any alternatives?

Answer

Ron Sijm picture Ron Sijm · Sep 24, 2013

For those that are googling this and ended up here like me, this is how you currently do it in EF5 and EF6:

context.Database.ExecuteSqlCommand("TRUNCATE TABLE [TableName]");

Assuming context is a System.Data.Entity.DbContext