问题
I am trying to eager load properties of a derived class in an Entity Framework model.
I read all over the place that I have to first filter the set with OfType() before including properties with Include():
var persons = Context.Persons
.OfType<Employee>()
.Include("Compensation")
I don't know how to get that Include() to work though because in my case, Persons is a DbSet, OfType() returns an IQueryable and IQueryable does not define an Include() method.
回答1:
Place this:
using System.Data.Entity;
into your using's list, and after that you will be able to use Include
extension methods family from DbExtensions class:
public static IQueryable<T> Include<T, TProperty>(this IQueryable<T> source, Expression<Func<T, TProperty>> path) where T : class;
public static IQueryable<T> Include<T>(this IQueryable<T> source, string path) where T : class;
public static IQueryable Include(this IQueryable source, string path);
They accept IQueryable as the first argument, and there are strongly-typed ones, too, which is better, then Include(String)
.
来源:https://stackoverflow.com/questions/12085353/how-to-use-include-after-oftype