2012-07-18 118 views
2

我通过DbContext生成器生成了我的实体,并将其添加到使用我的实体上下文模型的API控制器。以下方法未能虽然编译:实体框架包含未能编译

public IEnumerable<casino> Getcasinos() 
    { 
     var casinos = db.casinos.Include(c => c.city).Include(c => c.state); 
     return casinos.AsEnumerable(); 
    } 

编译器说:

Cannot Convert Lambda Expression to Type 'String' Because It Is Not A Delegate Type 

任何想法,为什么它是这样说?我有导入的System.Linq名称空间。

回答

8

这实际上是因为ObjectQuery(T).Include方法。这样做的函数签名:

public ObjectQuery<T> Include(string path); 

为什么你看到这可能是因为无论你叫它不具备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); 
    } 
} 

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

+0

工作很好!谢谢! – 2012-07-18 01:01:40

+0

@JuniperAsh:没问题。如果你在一个单独的层中完成数据访问,这实际上是一个非常容易犯的错误。 – 2012-07-18 01:02:19