[英]Compiled Query error: there is no implicit conversion from entities to 'System.Data.Objects.ObjectContext
我正在創建一個委托來從數據庫中檢索所有客戶記錄。 我已經以這種方式使用了編譯查詢,但出於某種原因,我在使用EF的Visual Studio 2012中遇到了這樣的錯誤。
錯誤:類型'HTML5Basics.NorthwindDataContext'不能在泛型類型或方法'System.Data.Objects.CompiledQuery.Compile(System.Linq.Expressions.Expression>)'中用作類型參數'TArg0'。 沒有從'HTML5Basics.NorthwindDataContext'到'System.Data.Objects.ObjectContext'的隱式引用轉換。
這個錯誤是什么以及如何解決這個錯誤?
這是代碼:
public static Func<NorthwindDataContext, string, IEnumerable<SimpleCustomer>> CustomersByCity =
CompiledQuery.Compile<NorthwindDataContext, string, IEnumerable<SimpleCustomer>>(
(NorthwindDataContext db, string city) =>
from c in db.Customers
where c.City == city
select new SimpleCustomer { ContactName = c.ContactName });
沒有從'HTML5Basics.NorthwindDataContext'到'System.Data.Objects.ObjectContext'的隱式引用轉換。
表示兩種類型之間沒有轉換。
在.NET 4.5,EF5中有一個System.Data.Objects命名空間,它包含CompiledQuery.Compile函數。 System.Data.Linq命名空間中還有一個。
他們有不同的簽名:
System.Data.Linq命名空間:(取自MSDN http://msdn.microsoft.com/en-us/library/bb548737.aspx ):
public static Func<TArg0, TResult> Compile<TArg0, TResult>(
Expression<Func<TArg0, TResult>> query)
where TArg0 : DataContext
System.Data.Objects命名空間(來自.pdb):
public static Func<TArg0, TResult> Compile<TArg0, TResult>
(Expression<Func<TArg0, TResult>> query)
where TArg0 : ObjectContext
基本上你有兩個選擇:
1)使用System.Data.Linq命名空間中的那個。
2)將ObjectContext(或繼承的類型)傳入System.Data.Objects命名空間的版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.