Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

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?

1 Answer

0 votes
by (40.7k points)

In Entity_Framework5 and Entity_Framework6 you can delete all rows in table this way:

Assuming context is a System.Data.Entity.DbContext

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

...