坚持实体框架查询缓存 [英] Persist Entity Framework query cache

查看:145
本文介绍了坚持实体框架查询缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

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