2012-07-18 125 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