使用RemoveRange()批量删除行 [英] Bulk deleting rows with RemoveRange()

查看:440
本文介绍了使用RemoveRange()批量删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从表中删除多行。

I am trying to delete multiple rows from a table.

在常规SQL Server中,这将很简单:

In regular SQL Server, this would be simple as this:

DELETE FROM Table
WHERE
    Table.Column = 'SomeRandomValue'
    AND Table.Column2 = 'AnotherRandomValue'



< us / library / system.data.entity.dbset.removerange%28v = vs.113%29.aspxrel =nofollow noreferrer> RemoveRange()方法。

但是,当我使用它,而不是使用我提供的where子句删除行,Entity Framework查询数据库以获取与where子句匹配的所有行,并使用其主键逐个删除。

In Entity Framework 6, they have introduced RemoveRange() method.
However, when I use it, rather than deleting rows using the where clauses that I provided, Entity Framework queries the database to get all rows that match the where clauses and delete them one by one using their primary keys.

这是EntityFramework目前的限制吗?
或者我使用 RemoveRange()错误?

Is this the current limitation of EntityFramework? Or am I using RemoveRange() wrong?

以下是我如何使用 RemoveRange()

db.Tables.RemoveRange(
    db.Tables
        .Where(_ => _.Column == 'SomeRandomValue'
            && _.Column2 == 'AnotherRandomValue')
);


推荐答案

我想我们在这里达到了EF限制。
有时你只需要使用 ExecuteSqlCommand 保持高效。

I think we reached here a limitation of EF. Sometimes you just have to use ExecuteSqlCommand to stay performant.

这篇关于使用RemoveRange()批量删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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