entity-framework - 在 Entity Framework 6 中的一个事务中删除多行时重复查询

标签 entity-framework entity-framework-6 mvc-mini-profiler

我正在使用 EF 6 并拥有这样的存储库类:

public class EfRepository<T> : IRepository<T> where T : class
{
    private readonly DbContext _context;

    public EfRepository(DbContext context)
    {
        _context = context;
    }
    ....
    public void Delete(IEnumerable<T> entities)
    {
        // skip checks
        using (var transaction = _context.Database.BeginTransaction())
        {
            try
            {
                _context.Set<T>().RemoveRange(entities);
                _context.SaveChanges();
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
            }
        }
    }

在我的 Controller 中,我有存储库实例 IRepository<Connection> _repository比用 Autofac 绑定(bind)到 EfRepository类。

然后我删除了多个项目(一切正常!):

IEnumerable<Connection> connections = // get some connections;
_repository.Delete(connections); // everything fine - records was removed

但是当我打开安装了 MiniProfiler 的网站时它向我显示重复的 sql 查询警告: MiniProfiler warning

我的问题是为什么我使用事务但仍然有重复的 sql 警告?

谢谢。

最佳答案

这是因为 Entity Framework 当前为每个要删除的项目发送一个查询。它不会将它们全部批处理到一个查询中。因此,MiniProfiler 正确报告正在发生的情况 - 正在提交重复的删除查询(参数值除外)。

关于entity-framework - 在 Entity Framework 6 中的一个事务中删除多行时重复查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24929039/

相关文章:

c# - 在时间范围内对获取项目进行分组

.net - SQL物理连接不可用

entity-framework - 数据库设计 : more tables vs less tables

c# - 在 Entity Framework 中使用 Postgis 的几何类型

xml - Azure部署: error MSB4041: The default XML namespace of the project must be the MSBuild XML namespace

asp.net - 在 Entity Framework 中的 2 个语句期间锁定表

mysql - 无法在 .NET Core 上使用 MiniProfiler 分析 MySQL

html - Mvc MiniProfiler 结果/chilets 被 DIV 隐藏/覆盖

asp.net-membership - MiniProfiler 和 SqlMembershipProvider

c# - 实体 SQL 比较没有毫秒的日期时间