删除Entity Framework中的所有实体 [英] Delete all entities in Entity Framework

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

问题描述

我想使用Entity Framework 4+删除所有表(所有实体)的内容。这样做可以如何做?

I want to delete content of all tables (all entities) using Entity Framework 4+. How can this be done?

推荐答案

这将比执行删除个人实体对象,假设底层数据库是MSSQL。

This will perform much, much better than anything involving deleting individual entity objects, assuming the underlying database is MSSQL.

foreach (var tableName in listOfTableNames)
{
    context.ExecuteStoreCommand("TRUNCATE TABLE [" + tableName + "]");
}

当然,如果您的表具有外键关系,则需要以正确的顺序设置表名列表,以便在清除可能依赖的主键表之前清除外键表。

Of course, if your tables have foreign-key relationships, you'll need to set up your list of table names in the proper order so that you clear foreign-key tables before you clear any primary-key tables that they might depend upon.

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

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