坚持实体框架查询缓存 [英] Persist Entity Framework query cache
问题描述
我有一个ASP.NET MVC 5 Web的应用程序,并使用EF 6.1来访问我的数据库。结果
我有一个长达10秒编译一些相当复杂的LINQ查询,但随后在几毫秒内执行。 EF不会缓存这个查询罚款,第二次执行查询它这个几毫秒内返回。结果
但这个缓存没有对每一个应用程序重启的查询需要重新编译,这需要其持续如此反复10秒。
I have an ASP.NET MVC 5 web-application and use EF 6.1 to access my DB.
I have some rather complex LINQ queries which take up to 10s to compile, but then execute in a few milliseconds. EF does cache this queries fine and the second time the query is executed it returns within this few milliseconds.
But this cache is not persisted so on every app-restart the query needs to be recompiled, which takes that 10s again.
的有没有办法坚持这个查询缓存,以便它生存的应用程序重启?的
推荐答案
You can use compiled queries: see here or here
static readonly Func<AdventureWorksEntities, Decimal, IQueryable<SalesOrderHeader>> s_compiledQuery2 =
CompiledQuery.Compile<AdventureWorksEntities, Decimal, IQueryable<SalesOrderHeader>>(
(ctx, total) => from order in ctx.SalesOrderHeaders
where order.TotalDue >= total
select order);
但是如前所述这里查询对象一定不能出去范围。您可以通过保持它在会话或应用程序变量缓存处理这个问题。
But as mentioned here the query object must not go out of scope. You can handle this by either keeping it cached in the session or as application variable.
这篇关于坚持实体框架查询缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!