Entity Framework Include () is strongly typed

Is there a way to make this strongly typed using the System.Data.Entity.Include method? In the method below, Escalation is an 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(); } 
+45
c # entity-framework
May 23 '11 at 20:54
source share
2 answers

It is already available in Entity Framework 4.1.

See here for help on how to use the include function, and also how to include several levels: http://msdn.microsoft.com/en-us/library/gg671236(VS.103).aspx

The strongly typed Include() method is an extension method, so you need to remember the declaration using System.Data.Entity; .

+83
Jun 01 '12 at 1:28
source share

The loan 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) 
+6
May 23 '11 at 20:57
source share



All Articles