删除 EF CTP5 中的多条记录
我有一个 Person 类:
public class Person {
public int Id { get; set; }
public int DeptId { get; set; }
public decimal Salary { get; set; }
}
在数据库中,Id 和 DeptId 都被标记为主键。
我试图从数据库中删除 DeptId 值与 x(输入变量)匹配的所有记录。
如果我像这样访问记录:
var people = Database.People.Where(p => p.DeptId = 10);
删除所有此类记录(不使用迭代)的最佳方法是什么?
I have a Person class:
public class Person {
public int Id { get; set; }
public int DeptId { get; set; }
public decimal Salary { get; set; }
}
In the database both Id and DeptId are marked as primary key.
I was trying to delete all records from the database where the DeptId value matches x (an input variable.)
If I access the records like this:
var people = Database.People.Where(p => p.DeptId = 10);
What is the best way to delete all such records (without using iteration)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您的 Person 具有来自 Id 和 DepId 的复杂 PK,所以您的模型应该包含识别关系。在这种情况下,您也可以这样做:
这与迭代中删除人员相同,因为 EF 无法批处理命令。它将删除到数据库的单独往返中的每个人。如果部门里真的有很多人,可以这样称呼:
Because your Person has complex PK from Id and DepId your model should contain identifying relation. In such case you can also do this:
It will be the same as deleting people in iteration because EF is not able to batch commands. It will delete each person in separate roundtrip to DB. If you have really lot of people in department call this: