Entity Framework. Delete all rows in table

前端 未结 21 1956
一生所求
一生所求 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:10

    if

          using(var db = new MyDbContext())
                {
                   await db.Database.ExecuteSqlCommandAsync(@"TRUNCATE TABLE MyTable"););
                }
    

    causes

    Cannot truncate table 'MyTable' because it is being referenced by a FOREIGN KEY constraint.

    I use this :

          using(var db = new MyDbContext())
                   {
                       await db.Database.ExecuteSqlCommandAsync(@"DELETE FROM MyTable WHERE ID != -1");
                   }
    

提交回复
热议问题