问题描述
我创建一个代表来检索数据库中的所有专辑的记录。我用在另一个项目中同样的方式,但由于某些原因,我得到一个错误这一次。
I'm creating a delegate to retrieve all album records in the database. I've used this same way in another project, but for some reason I'm getting an error this time.
有我错过了一步?我不知道为什么这个错误出现。
Have I missed a step? I'm not sure why this error is appearing.
code
public static readonly Func<CodySolutionEntities, IQueryable<Album>> SelectAlbums =
CompiledQuery.Compile<CodySolutionEntities, IQueryable<Album>>(
query => from q in query.Albums.Include("Photo")
select q);
错误
错误1型'CodyData.Diagram.CodySolutionEntities不能用作泛型类型或方法的类型参数'TArg0 'System.Data.Objects.CompiledQuery.Compile<TArg0,TResult>(System.Linq.Ex$p$pssions.Ex$p$pssion<System.Func<TArg0,TResult>>)'.有一个从CodyData.Diagram.CodySolutionEntities'到'System.Data.Objects.ObjectContext'隐式引用转换。 C:\\用户\\科迪\\文档\\ CMBS \\ CodySolution \\ CodyData \\代表\\ PhotoDelegates.cs 13 13 CodyData code>
Error 1 The type 'CodyData.Diagram.CodySolutionEntities' cannot be used as type parameter 'TArg0' in the generic type or method 'System.Data.Objects.CompiledQuery.Compile<TArg0,TResult>(System.Linq.Expressions.Expression<System.Func<TArg0,TResult>>)'. There is no implicit reference conversion from 'CodyData.Diagram.CodySolutionEntities' to 'System.Data.Objects.ObjectContext'. C:\Users\Cody\Documents\CMBS\CodySolution\CodyData\Delegates\PhotoDelegates.cs 13 13 CodyData
推荐答案
该错误信息表明 CodySolutionEntities
不是从的ObjectContext $派生C $ C>,这是一个问题,因为
CompiledQuery
只能用的ObjectContext
的作品。在这种情况下 CodySolutionEntities
必须从的DbContext
对象当中,在这个时候,是的。
The error message indicates that CodySolutionEntities
is not derived from ObjectContext
, which is a problem because the CompiledQuery
only works with an ObjectContext
. In this case CodySolutionEntities
must be derived from a DbContext
object which, at this time, is the recommended context object to use.
的 CompiledQuery
大概是因为,实体框架的版本之前创建从的ObjectContext
派生的对象4.1曾在过去而不是的DbContext
为您管理您的实体。
The CompiledQuery
probably worked in the past because, Entity Framework versions prior to 4.1 created an object derived from ObjectContext
instead of DbContext
for you to manage your entities.
如后解释说,如果你能的目标。 NET 4.5你可以使用EF 5,你不会需要 CompiledQuery
了,因为它会自动缓存编译LINQ到实体查询为您服务。如果没有,你可能要考虑切换回使用的ObjectContext
,但这样做,要确保你的心态不只是之前的。
Like this post explains, if you're able to target .NET 4.5 you can use EF 5, and you won't need the CompiledQuery
anymore as it will automatically cache compiled LINQ to Entity queries for you. If not you may want to look into switching back to using an ObjectContext
, but before doing that make sure your mindset isn't simply compiled or bust.
这篇关于编译的查询隐式引用转换的ObjectContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!