C# – Entity Framework. Delete all rows in table

centity-frameworklinqsql

How can I quickly remove all rows in the 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?

Best Answer

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