Persist Entity Framework query cache(持久化实体框架查询缓存)
问题描述
我有一个 ASP.NET MVC 5 Web 应用程序并使用 EF 6.1 访问我的数据库.
我有一些相当复杂的 LINQ 查询,编译时间长达 10 秒,然后在几毫秒内执行.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.
这篇关于持久化实体框架查询缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:持久化实体框架查询缓存
基础教程推荐
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
