I’ve never had a problem with any changes to the db not being reflected when I make a repository call on the data before. But this now seems to be happening.
Before, such repository calls have always been in the context of using the repository pattern where the repository i/fs are injected into the service/controller constructors.
I have an email task written as a console app, containing a while() loop, which interrogates the database for email accounts and then downloads emails from the respective servers. The problem is, every time I make a call to the _emailSettingsRepo.GetAll() method, all of the settings are old and don’t show any changes to the database even though I can see the change in management studio. I’m guessing this is because I’m using the same db context by using the same repo inside the loop, so making the GetAll() method call doesn’t make any difference.
Does this mean that the caching has never been a problem before as the repo and therefore db context is re-created each time a new request comes into the controller? How do I get round this problem?
Create a context when you need it and dispose it afterwards, something like:
Comparing this console app with a web app regarding context management, think that every run through the loop is a web request. If you are using an IOC container in your web app (for example Ninject) it is well possible that the container creates the context and disposes it for you. But it is always a pair of
context = new MyContext()andcontext.Dispose()and the request processing in between. So, basically the same like theusingblock in the example above. You just have to do it explicitely now in your console app.