Bulk deleting rows with RemoveRange()(使用 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'
在 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() 批量删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 RemoveRange() 批量删除行
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
