如何预编译实体框架代码优先查询? [英] How do I precompile an Entity Framework Code-First Query?

查看:127
本文介绍了如何预编译实体框架代码优先查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一些性能问题,我的实体框架代码第一个查询,我相信预编译可能是答案。如果我使用正常实体框架,我只需使用 CompiledQuery.Compile 方法来预先查询我的查询。但是由于我有一个DbContext而不是一个ObjectContext,所以我无法让这个工作。

I am encountering some performance problems with my Entity Framework Code-First queries and I believe that precompilation may be the answer. If I were using "normal" Entity Framework, I would simply use the CompiledQuery.Compile method to precomiple my queries. But since I have a DbContext and not an ObjectContext, I can't get this to work.

我意识到DbContext是一个 IObjectContextAdapter ,这让我可以访问ObjectContext,但是我找不到可以从我的预编译查询中使用的对象上下文得到一个IQueryable的方法。我尝试使用 CreateObjectSet ,但是当EF尝试运行查询时抱怨说它无法将该方法转换成SQL。

I do realize that DbContext is an IObjectContextAdapter, which gives me access to the ObjectContext, but I cannot find the method that lets me get an IQueryable from my object context that works in my precompiled query. I tried to use CreateObjectSet, but when EF tried to run the query it complained that it couldn't convert that method into SQL.

那么,什么是最好的方法来预编译一个Code-First DbContext的LINQ查询?

推荐答案

从官方宣布:

没有编译来自DbContext
的查询支持不幸的是,由于我们在.NET Framework 4.0中发布的编译查询功能的一些技术限制,我们无法通过DbContext API支持编译查询,我们意识到这是一个痛苦的限制,并且可以启用这是下一个版本。

"No compiled query support from DbContext Unfortunately due to some technical limitations in the compiled query functionality we shipped in .NET Framework 4.0 we are unable to support compiled queries via the DbContext API. We realize this is a painful limitation and will work to enable this for the next release. "

Link

这篇关于如何预编译实体框架代码优先查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆