提问人:Emanuel 提问时间:10/4/2023 最后编辑:Svyatoslav DanylivEmanuel 更新时间:10/4/2023 访问量:28
如何在 EF Core 中将内存中缓存与预先加载配合使用
How to use In-Memory Cache with eager loading in EF Core
问:
我使用 In-Mmemory Cache 来存储一些数据库模型,并且我正在尝试使用 .Include() 中。这个想法不是更改我缓存模型的所有查询,而是以某种方式更新 Include() 方法,以便 EF Core 从缓存中读取该数据。
我尝试了这个例子,但我遇到了一些错误:
public static Expression<Func<MyEntity, IEnumerable<MySecondEntity>>> MyIncludeExpression
{
get { return e => CacheHelper.Get<List<MySecondEntity>>(CacheKeys.MySecondEntityKey).AsQueryable(); }
}
...
dbContext.MyEntity
.Where(i => id = 1)
.Include(i => MyIncludeExpression)
.FirstOrDefault();
错误:
表达式“e => System.Collections.Generic.List'1[MySecondEntity]' 在”Include“操作中无效,因为它不表示属性访问:”t => t.MyProperty”。若要以在派生类型上声明的导航为目标,请使用强制转换 ('t => ((Derived)t)。MyProperty') 或 'as' 运算符 ('t => (t as Derived)。MyProperty“)。可以通过组合 Where、OrderBy(Descending)、ThenBy(Descending)、Skip 或 Take 操作来筛选集合导航访问。有关包含相关数据的详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=746393。
有什么办法可以做到吗? 谢谢!
答: 暂无答案
评论
Invoke
.Include(i => MyIncludeExpression.Invoke(t))