问题描述
我正在尝试从表中删除多行.
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'
在 Entity Framework 6 中,他们引入了 RemoveRange() 方法.
但是,当我使用它时,Entity Framework 不是使用我提供的 where 子句删除行,而是查询数据库以获取与 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()
的方式:
Following is how I am using 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() 批量删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!