I'm using EF6 + WPF with MVVM design pattern in my desktop application. I'm using also Autofac as DI container.
I read a lot about EF context lifetime management and I decided to have only one instane of EF context for single view model instance. I found several interesting articles about that approach, so I thought that it is only good way to manage EF context. I use Autofac to manage EF lifetime, so every time I create new view model, there will be created only one new EF context.
But of course, I came across a problem. Most of my EF queries work good, but the following query always returns old (cached) value. I call this query every time I push 'Execute' button, so there are many executions per single view / view model
this.context.someTable.Where(arg => arg.value == "value").Single();
I know that I can always reload the entity with following code
this.context.Entry(entity).Reload();
But for me it is not a good solution. I know also that if I dispose current context and recreate before next query I will always receive current values. But this approach conflicts with one context per one view model approach.
What should I fix / change to avoid EF caching issues and still have good performance.
See Question&Answers more detail:os