删除实体框架的所有实体实体、框架

由网友(孤独┟┺┲┨死亡)分享简介:我要删除所有表的内容(使用实体框架4+所有实体)。如何才能做到这一点?I want to delete content of all tables (all entities using Entity Framework 4+).How can this be done?谢谢,詹姆斯推荐答案这将执行了,多的...

我要删除所有表的内容(使用实体框架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.

阅读全文

相关推荐

最新文章