Entity Framework. Delete all rows in table

前端 未结 21 2045
一生所求
一生所求 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 01:58

    Delete all records. Do not reset the primary index like "truncate".

    /// 
    /// SET - DELETE all record by table - no truncate - return deleted records
    /// 
    public static int setListDelAllMYTABLE()
    {
        // INIT
        int retObj = 0;
        using (MYDBEntities ctx = new MYDBEntities())
        {
            // GET - all record
            var tempAllRecord = ctx.MYTABLE.ToList();
            // RESET
            ctx.MYTABLE.RemoveRange(tempAllRecord);
            // SET - final save
            retObj += ctx.SaveChanges();
        }
        // RET
        return retObj;
    }
    

提交回复
热议问题