Is there a way to make this strongly typed using the System.Data.Entity.Include method? In the method below Escalation is a ICollection<>.
public IEnumerable<EscalationType> GetAllTypes() {
Database.Configuration.LazyLoadingEnabled = false;
return Database.EscalationTypes
.Include("Escalation")
.Include("Escalation.Primary")
.Include("Escalation.Backup")
.Include("Escalation.Primary.ContactInformation")
.Include("Escalation.Backup.ContactInformation").ToList();
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This is already available in Entity Framework 4.1.
See here for a reference for how to use the include feature, it also shows how to include multiple levels: http://msdn.microsoft.com/en-us/library/gg671236(VS.103).aspx
The strongly typed Include() method is an extension method so you have to remember to declare the using System.Data.Entity; statement.
Method 2
Credit goes to Joe Ferner:
public static class ObjectQueryExtensionMethods {
public static ObjectQuery<T> Include<T>(this ObjectQuery<T> query, Expression<Func<T, object>> exp) {
Expression body = exp.Body;
MemberExpression memberExpression = (MemberExpression)exp.Body;
string path = GetIncludePath(memberExpression);
return query.Include(path);
}
private static string GetIncludePath(MemberExpression memberExpression) {
string path = "";
if (memberExpression.Expression is MemberExpression) {
path = GetIncludePath((MemberExpression)memberExpression.Expression) + ".";
}
PropertyInfo propertyInfo = (PropertyInfo)memberExpression.Member;
return path + propertyInfo.Name;
}
}
ctx.Users.Include(u => u.Order.Item)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0