Entity Framework. Delete all rows in table

前端 未结 21 2070
一生所求
一生所求 2020-11-28 01:25

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;
forea         


        
21条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 02:05

    Using SQL's TRUNCATE TABLE command will be the fastest as it operates on the table and not on individual rows.

    dataDb.ExecuteStoreCommand("TRUNCATE TABLE [Table]");
    

    Assuming dataDb is a DbContext (not an ObjectContext), you can wrap it and use the method like this:

    var objCtx = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)dataDb).ObjectContext;
    objCtx.ExecuteStoreCommand("TRUNCATE TABLE [Table]");
    

提交回复
热议问题