使用EF从数据库查询是否正确?
本文关键字:是否 查询 EF 数据库 使用 | 更新日期: 2023-09-27 18:15:33
我可以问这是否是使用EF删除整个数据库表的正确方法吗?Department表连接Facility和departmentd,Facility表连接Facility Reservation和FacilityID,我需要删除Facility Reservation表中的所有记录。有人可以指导我,告诉我这是正确的吗?谢谢你!
db.Database.ExecuteSqlCommand(
"DELETE FacilityReservation FROM Department INNER JOIN Facility ON Department.DepartmentID = Facility.DepartmentID " +
"INNER JOIN FacilityReservation ON Facility.FacilityID = FacilityReservation.FacilityID WHERE Department.DepartmentID =" + departmentID);
不,当然不是,您需要DELETE FROM "。
你可能想要这个:
DELETE FROM FacilityReservation
INNER JOIN Facility
ON Facility.FacilityID = FacilityReservation.FacilityID
INNER JOIN Department
ON Department.DepartmentID = Facility.DepartmentID
WHERE Department.DepartmentID = @departmentID;
从SQL语句中,看起来可以从Facility表中检索DepartmentID。因此,您的SQL语句可以相当简单,如:
DELETE a
FROM FacilityReservation a
INNER JOIN Facility b ON a.FacilityID = b.FacilityID
WHERE b.DepartmentID = @departmentID