来自数据库使用EF的查询是否正确?

时间:2015-10-02 07:48:27

标签: c# mysql sql entity-framework

我可以问这是否是使用EF删除整个数据库表的正确方法? 部门表与DepartmentID连接设施, 设施表使用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);

2 个答案:

答案 0 :(得分:1)

不,当然不是,您需要DELETE FROM< TABLENAME>,当您进行连接时,您需要另一个“FROM< Table>”。

你可能想要这个:

DELETE FROM FacilityReservation 

INNER JOIN Facility 
    ON Facility.FacilityID = FacilityReservation.FacilityID 

INNER JOIN Department 
    ON Department.DepartmentID = Facility.DepartmentID 

WHERE Department.DepartmentID = @departmentID;

答案 1 :(得分:0)

从您的SQL语句中,可以从Facility表中检索DepartmentID。因此,您的SQL语句可能非常简单,例如:

DELETE a
FROM FacilityReservation a
INNER JOIN Facility b ON a.FacilityID = b.FacilityID
WHERE b.DepartmentID = @departmentID

See SQL Fiddle here.