首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实体框架中的批量删除

问实体框架中的批量删除
EN

Stack Overflow用户
提问于 2012-07-21 20:27:29
回答 5查看 16.1K关注 0票数 5

我想使用linq从表中批量删除记录。有一篇文章描述了如何做到这一点:Bulk-deleting in LINQ to Entities

代码语言:javascript
复制
var query = from c in ctx.Customers
            where c.SalesPerson.Email == "..."
            select c;

query.Delete();

但是函数"Delete“并不存在于我的var变量中。

此外,在我的上下文中不存在函数"SubmitChanges“。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-07-21 21:23:43

lets you do batch deletes and updates中有一个有趣的NuGet包

票数 4
EN

Stack Overflow用户

发布于 2012-07-21 21:27:34

Entity Framework中当前没有支持的批量删除。这实际上是codeplex上正在讨论的特性之一,现在EF是开源的。

EntityFramework.Extended提供了批量删除支持(你可以在nuget中找到),但是我的经验是它有一些性能问题。

票数 4
EN

Stack Overflow用户

发布于 2014-03-26 19:49:39

此代码向任何DbContext添加一个简单的扩展方法,该方法将批量删除您提供的实体框架查询中引用的任何表中的所有数据。它的工作方式是提取查询中涉及的所有表名,并尝试通过发出"DELETE FROM tablename“SQL查询来删除数据,这在大多数类型的数据库中都很常见。

要使用,只需执行以下操作:

代码语言:javascript
复制
myContext.BulkDelete(x => x.Things);

这将删除链接到Things实体存储的表中的所有内容。

代码:

代码语言:javascript
复制
using System.Linq;
using System.Text.RegularExpressions;

namespace System.Data.Entity {

    public static class DbContextExtensions {

        /// <summary>
        /// Efficiently deletes all data from any database tables used by the specified entity framework query. 
        /// </summary>
        /// <typeparam name="TContext">The DbContext Type on which to perform the delete.</typeparam>
        /// <typeparam name="TEntity">The Entity Type to which the query resolves.</typeparam>
        /// <param name="ctx">The DbContext on which to perform the delete.</param>
        /// <param name="deleteQuery">The query that references the tables you want to delete.</param>
        public static void BulkDelete<TContext, TEntity>(this TContext ctx, Func<TContext, IQueryable<TEntity>> deleteQuery) where TContext : DbContext {

            var findTables = new Regex(@"(?:FROM|JOIN)\s+(\[\w+\]\.\[\w+\])\s+AS");
            var qry = deleteQuery(ctx).ToString();

            // Get list of all tables mentioned in the query
            var tables = findTables.Matches(qry).Cast<Match>().Select(m => m.Result("$1")).Distinct().ToList();

            // Loop through all the tables, attempting to delete each one in turn
            var max = 30;
            var exception = (Exception)null;
            while (tables.Any() && max-- > 0) {

                // Get the next table
                var table = tables.First();

                try {
                    // Attempt the delete
                    ctx.Database.ExecuteSqlCommand(string.Format("DELETE FROM {0}", table));

                    // Success, so remove table from the list
                    tables.Remove(table);

                } catch (Exception ex) {
                    // Error, probably due to dependent constraint, save exception for later if needed.                    
                    exception = ex;

                    // Push the table to the back of the queue
                    tables.Remove(table);
                    tables.Add(table);
                }
            }

            // Error error has occurred, and cannot be resolved by deleting in a different 
            // order, then rethrow the exception and give up.
            if (max <= 0 && exception != null) throw exception;

        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11592176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档