How do I precompile an Entity Framework Code-First Query?

非 Y 不嫁゛ 提交于 2019-12-17 12:49:39

问题


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.

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.

So what is the best way to precompile LINQ queries against a Code-First DbContext?


回答1:


As from the official announecment:

"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.




回答2:


This will be probably solved in EFv4.2 EF vNext (currently in the very first CTP) by auto-compiled LINQ queries.



来源:https://stackoverflow.com/questions/6426508/how-do-i-precompile-an-entity-framework-code-first-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!