我在我的桌面应用程序中使用EF6 + WPF和MVVM设计模式.我也使用Autofac作为DI容器.
我读了很多关于EF上下文生命周期管理的内容,我决定只为单一视图模型实例提供一个EF上下文实例.我发现了一些关于这种方法的有趣文章,所以我认为这只是管理EF上下文的好方法.我使用Autofac来管理EF生命周期,因此每次创建新的视图模型时,都只会创建一个新的EF上下文.
但当然,我遇到了一个问题.我的大多数EF查询都运行良好,但以下查询始终返回旧(缓存)值.每次按下"执行"按钮时都会调用此查询,因此每个视图/视图模型有很多执行
this.context.someTable.Where(arg => arg.value == "value").Single();
Run Code Online (Sandbox Code Playgroud)
我知道我总是可以使用以下代码重新加载实体
this.context.Entry(entity).Reload();
Run Code Online (Sandbox Code Playgroud)
但对我来说这不是一个好的解决方案.我也知道,如果我处理当前上下文并在下一个查询之前重新创建,我将始终接收当前值.但是这种方法与每种视图模型方法的一个上下文冲突.
我应该修复/更改什么以避免EF缓存问题并且仍然具有良好的性能.
我有.NET库的二进制文件,我必须恢复所有源.我使用.NET Reflector,在大多数情况下它运行得很好.但现在我有动态类型的问题.我有以下代码,我现在不知道如何理解它.
dynamic obj2 = component;
if (<SetValue>o__SiteContainer0<T>.<>p__Site1 == null)
{
<SetValue>o__SiteContainer0<T>.<>p__Site1 = CallSite<Func<CallSite, object, bool>>.Create(Binder.UnaryOperation(CSharpBinderFlags.None, ExpressionType.IsTrue, typeof(RecordPropertyDescriptor<T>), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));
}
if (<SetValue>o__SiteContainer0<T>.<>p__Site2 == null)
{
<SetValue>o__SiteContainer0<T>.<>p__Site2 = CallSite<Func<CallSite, object, object, object>>.Create(Binder.BinaryOperation(CSharpBinderFlags.None, ExpressionType.NotEqual, typeof(RecordPropertyDescriptor<T>), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null) }));
}
if (<SetValue>o__SiteContainer0<T>.<>p__Site1.Target(<SetValue>o__SiteContainer0<T>.<>p__Site1, <SetValue>o__SiteContainer0<T>.<>p__Site2.Target(<SetValue>o__SiteContainer0<T>.<>p__Site2, obj2[this.Name], value)))
{
// some simple code
}
Run Code Online (Sandbox Code Playgroud)
任何sugestions?
编辑
我使用ilspy.net,对我来说情况更糟
Func<CallSite, object, bool> arg_163_0 = RecordPropertyDescriptor<T>.<SetValue>o__SiteContainer0.<>p__Site1.Target;
CallSite arg_163_1 = RecordPropertyDescriptor<T>.<SetValue>o__SiteContainer0.<>p__Site1;
if (RecordPropertyDescriptor<T>.<SetValue>o__SiteContainer0.<>p__Site2 == null)
{ …Run Code Online (Sandbox Code Playgroud)