实体框架包含无法编译

Log*_*man 2 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();
    }
Run Code Online (Sandbox Code Playgroud)

编译器说:

Cannot Convert Lambda Expression to Type 'String' Because It Is Not A Delegate Type
Run Code Online (Sandbox Code Playgroud)

有什么想法为什么这么说?我System.Linq导入了名称空间.

Mik*_*ley 8

这实际上是因为该ObjectQuery(T).Include方法而发生的.这有功能签名:

public ObjectQuery<T> Include(string path);
Run Code Online (Sandbox Code Playgroud)

您之所以看到这种情况,可能是因为无论您在何处调用它都没有System.Data.Entity可用的命名空间.从DbExtensions元数据中可以看出Include使用表达式需要System.Data.Entity命名空间:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果包含System.Data.Entity命名空间,则错误将解决.