Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架Include无法编译_Entity Framework_Asp.net Web Api - Fatal编程技术网

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);
    }
}