1
  1. I fetch the object from the database (POCO object with proxy)
  2. I modify its property without saving changes
  3. I fetch it later on again from the database (I use the same object context)
  4. What is the value of the property?

For example if I have object context with collection Users then what is the result of the following:

ObjectContext o = ....;
User u = o.Users.First(u=>u.Id == 1);
Console.WriteLine(u.LastName); // I get 'test' for example
u.LastName = 'SomethingElse';
u  = o.Users.First(u=>u.Id == 1);
Console.WriteLine(u.LastName); // What is the result??

If the result of the last statement is not the string "SomethingElse" then is there a way to achieve this funcionality?

Dalibor Čarapić
  • 2,227
  • 18
  • 31

2 Answers2

1

That is the core feature of ORM tools called identity map pattern. Entity with unique key can be materialized only once per context so the second query will be default use the same instance without changing its values.

You can force the query to refresh values by either:

ObjectContext o = ....;
User u = o.Users.First(u=>u.Id == 1);
Console.WriteLine(u.LastName); // I get 'test' for example
u.LastName = 'SomethingElse';
o.Users.MergeOption = MergeOption.OverwriteChagnes; // Here you change the behavior
u  = o.Users.First(u=>u.Id == 1);
Console.WriteLine(u.LastName); // result is 'test' again

or by reloading the entity itself:

ObjectContext o = ....;
User u = o.Users.First(u=>u.Id == 1);
Console.WriteLine(u.LastName); // I get 'test' for example
u.LastName = 'SomethingElse';
o.Refresh(RefreshMode.StoreWins, u); // Here you reload entity with values from the database
Console.WriteLine(u.LastName); // result is 'test' again    
Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
0

Yes, there is an identity-manager that returns the same object if it sees the same record identity come back from the database.

As such, the result should be "SomethingElse", unless you disable the identity-manager.

Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
  • or use `Detach`... (which i've had to do, particulary after a POST when i need a trigger-generated slug) – RPM1984 Jul 21 '11 at 08:17