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

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

问题描述

如何使用实体框架快速删除表中的所有行?

How can I quickly remove all rows in the 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天全站免登陆