views:

99

answers:

2
+2  A: 

What is the life-cycle of the data-context? Is it the same data-context, or a new one each time? (it should probably be the latter).

If you re-use a data-context, it is obliged to always give you back the same object when ever it seems the same identity. So if it has previously given you the version with 9, it'll keep giving you back that reference even when the underlying data changes.

At the moment, this still does a round-trip; to avoid the round-trip, you need to use:

var obj = ctx.SomeTable.Single(x=>x.Id == id);

i.e. if it already has the object with Id == id in the local identity cache, it won't perform a databae query at all.

any other construct will do a round trip. There is a fix in 4.0 so that .Where(x=>x.Id == id).Single() will avoid the round-trip, which should have with query syntax, i.e.

var obj = (from x in ctx.SomeTable
           where x.Id == id
           select x).Single();
Marc Gravell
I was creating a partial class which I would then expose IQueryable methods. This was using the same datacontext, and as you quite rightly say, this will return back the same object. So I've now created a new instance of the datacontext on the page itself and its working. Just out of curiosity, is my first approach feasible? - As I would very much prefer to keep all methods constrained to a partial class for easy updating across my whole application. Thanks very much.
Robsimm
If you are re-using a data-context *between* pages, you are going to have huge problems with threading and locking; and the identity tracking + change tracking means you'll slowly clog everything up. In short; data-contexts are meant to be short-lived and localised.
Marc Gravell
+1  A: 

Are you updating using one DataContenxt while the one you later query from is still alive?

If yes this is the respones you should expect.

If you created a new DataContenxt after the update or the update and the read are being done on the same DataContext you shoud get the right response.

Sruly