0

Suppose I have models (and tables) that look like below:

Program: ProgramId (PKey)
ProgramRegistration: ProgramRegistrationId (PKey), ProgramId (FKey)
CourseEnrollments: CourseEnrollmentId (PKey), ProgramRegistrationId (FKey)

What I need to do is that in a controller I am passing an id that happens to be the ProgramId. I want to bulk delete all rows (or items) in CourseEnrollments that have a matching record in ProgramRegistration with the passed parameter id = ProgramId.

So I figured I would do:

db.CourseEnrollments.Where(e => e.ProgramRegistration.ProgramId == id).ToList().Remove();

Question: Is the above approach of using navigation properties correct?

1 Answer 1

2

I solved it in the following manner instead:

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that's correct. Almost the only way. In extensions methods style it will be foreach (var c in db.CourseEnrollments.Where(e => e.ProgramRegistration.ProgramId == id). They are equal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.