实体框架。删除表中的所有行 [英] Entity Framework. Delete all rows in table

查看:90
本文介绍了实体框架。删除表中的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能使用实体框架快速删除表中所有行?

How I can quickly remove all rows in table using Entity Framework?

我目前使用的:

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.

有什么办法?

推荐答案

有关那些谷歌搜索这一点,并在这里结束了像我这样的,这是你目前如何做到这一点的EF5和EF6:

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]");

假设上下文是 System.Data.Entity.DbContext

这篇关于实体框架。删除表中的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆