Entity framework 实体框架Include无法编译
我通过Entity framework 实体框架Include无法编译,entity-framework,asp.net-web-api,Entity Framework,Asp.net Web Api,我通过DbContext生成器生成实体,并将其添加到使用实体上下文模型的API控制器中。但以下方法无法编译: public IEnumerable<casino> Getcasinos() { var casinos = db.casinos.Include(c => c.city).Include(c => c.state); return casinos.AsEnumerable(); } 你知道它为什么这么说吗?我导
DbContext
生成器生成实体,并将其添加到使用实体上下文模型的API控制器中。但以下方法无法编译:
public IEnumerable<casino> Getcasinos()
{
var casinos = db.casinos.Include(c => c.city).Include(c => c.state);
return casinos.AsEnumerable();
}
你知道它为什么这么说吗?我导入了
System.Linq
名称空间。这实际上是因为ObjectQuery(T).Include
方法。它具有以下功能签名:
public ObjectQuery<T> Include(string path);
如果包含
System.Data.Entity
名称空间,错误将得到解决。@JuniperAsh:没问题。如果将数据访问封装在一个单独的层中,这实际上是一个非常容易犯的错误。
public ObjectQuery<T> Include(string path);
namespace System.Data.Entity
{
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Justification = "Casing is intentional")]
public static class DbExtensions
{
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);
}
}