views:

95

answers:

1

I created my own context which inherits from DbContext. Let's assume I have 1 post in my [Posts] table in the database. Consider this scenario:

  1. I ask DbContext for this only post for the first time. And DbContext returns it as expected.
  2. I change one column in [Posts] table manually.
  3. I refresh my site = I ask DbContext for this post again.
  4. DbContext returns a post which has old value for this specific column!

I looked into SQL Profiler and the database IS hit every time I refresh my site, so why the returned post has an old value? I guess DbContext is trying to be very clever and has some internal caching mechanism, but it would be nice to inform me that he's so clever...

Can someone explain this to me?

+1  A: 

Your question is divided into two already asked questions.

Caching problem: http://stackoverflow.com/questions/3617987/ef-4-0-model-caching-the-data-and-does-not-detect-the-modified-data/3621477#3621477

Shared context problem: http://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653392#3653392

Ladislav Mrnka
Thanks, this was very helpful since I didn't know about Identity Map pattern. I still don't understand 1 thing - I use StructureMap to create my context object and I didn't change default scope in StructureMap config. So from what I understand, new context should be created per request. But it's not! I set a breakpoint in the code and every time I refresh the site, the same instance of the context is provided...
Darmak
That is for a new question about StructureMap and DbContext in web application. I have never used StructureMap so I'm not able to help you.
Ladislav Mrnka
True, I just thought you could provide a quick answer :) Anyway, thanks very much, I'm a smarter man now (hopefully).
Darmak