假设我的模型(和表格)如下所示:
Program: ProgramId (PKey)
ProgramRegistration: ProgramRegistrationId (PKey), ProgramId (FKey)
CourseEnrollments: CourseEnrollmentId (PKey), ProgramRegistrationId (FKey)
我需要做的是在控制器中我传递的ID恰好是ProgramId。我想批量删除CourseRempments中在ProgramRegistration中具有匹配记录的所有行(或项)与传递的参数id = ProgramId。
所以我想我会这样做:
db.CourseEnrollments.Where(e => e.ProgramRegistration.ProgramId == id).ToList().Remove();
问题:上述使用导航属性的方法是否正确?
答案 0 :(得分:2)
我用以下方式解决了它:
var results = from c in vm.CourseEnrollments
where c.ProgramRegistration.ProgramId == id
select c;
foreach (var courseenrollment in results)
{
db.CourseEnrollments.Remove(courseenrollment);
}
db.SaveChanges();