c# - EF Core 单个 DELETE 语句
问题描述
在断开连接的情况下,我有以下模型:
public class Module
{
public Module()
{
Students = new List<Student>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
public string Name { get; set; }
public List<Student> Students { get; set; }
}
public class Student
{
public Student()
{
Modules = new List<Module>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
public string Name { get; set; }
public List<Module> Modules { get; set; }
}
我正在调用 RemoveStudents 并在下面传递两个学生 ID 以从模块中删除一些学生
public void RemoveStudents(List<long> studentIds)
{
var module = (from dbModule in dbContext.Module.Include(x => x.Students)
select dbModule).SingleOrDefault(x => x.Id == 1);
module.Students.RemoveAll(x => studentIds.Contains(x.Id));
dbContext.SaveChanges();
}
我可以看到它生成两个删除查询(每个学生一个)。
DELETE FROM [ModuleStudent]
WHERE [ModulesId] = @p0 AND [StudentsId] = @p1;
出于性能原因,SQL 中最好编写一个包含所有 Id 的查询:
DELETE FROM [ModuleStudent]
WHERE [ModulesId] = @p0 AND [StudentsId] IN (@p1, @p2, @p3.....);
如何“强制” EF 使用多个参数生成删除?
解决方案
推荐阅读
- java - 当我们使用 Kodein Aware 框架时,是否也需要使用 Dagger?
- javascript - 边界改变后清除标记,vue cookbook google map
- angular - 从角度 9 中的一个组件单击按钮时显示组件
- regex - 如何匹配包含多个给定单词的字符串?
- qt - 达到屏幕限制后,突出显示矩形被放置在列表元素后面
- docker - kubectl 公开部署 hello-minikube --type=NodePort 不工作
- python - 如何使用 NLTK 从 Python 中的文本字符串中提取数字
- excel - 如何使用表单(方法=发布)登录网站?
- python - PyQt 使用一键将温度转换为摄氏度和华氏度
- python - 比较两个图像(包含建筑物)的相似性的最有效方法是什么