Consider the following code, which is calling against an EF generated data context:
var context = new DataContext();
var employees = context.Employees.Include("Department");
If I change the name of the Department relationship then this code is going to start throwing a runtime error. So is there any way to call the .Include() method in a safe manner, so I get compile time checking for all the relationships being referenced?
Taking moi_meme’s idea a step further, my colleague developed the following solution that works in all cases. He introduced a new method caled
Includes()for dealing with one-to-many and many-to-many relationships. It allows you to write this:as this:
All credit goes to https://stackoverflow.com/users/70427/bojan-resnik, so go give him some love if you like the solution.