0

My WP app can edit data from database, which I get using LINQ query:

from item in db.SomeTable
where item.Id = 5
select new { Item = item}

And then I bind that selection to form controls. When I don't want to save changes (no db.SubmitChanges() call), I call another LINQ to reset field, but I get changed source values from db (even when I'm using select new {} operator. If there any way to reset to default state, so that I can get default values using LINQ again?

Bibhu
  • 4,053
  • 4
  • 33
  • 63
Wayne
  • 333
  • 1
  • 8
  • 19
  • http://stackoverflow.com/questions/5466677/undo-changes-in-entity-framework-entities – edze Sep 06 '13 at 08:02

1 Answers1

0

If you're using the same instance of the DataContext, you will get the latest version of any entities within it. You might want to consider changing your application to keep the DataContexts open for the minimum amount of time

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
  • You mean that I need to create new instance of DataContext on every page? Not global one for all app? I thought, that when I'm using `new` operator LINQ looses link with `DataContext`. – Wayne Sep 06 '13 at 07:55
  • @Wayne `new` is there for projecting purposes, it has nothing to do with the database values – anouar.bagari Sep 06 '13 at 07:58
  • Your `new` is creating an object that *references the one from the `DataContext`* - you *could* call a copy constructor to read a copy of the current object, but then pushing edits back is going to be more work. – Rowland Shaw Sep 06 '13 at 08:01
  • @Wayne - You should look into the following SO answer. Essentially it creates a single DataContext *per request*. You can tailor this to per page or even in a specific method if necessary. A global static DataContext will track changes across requests which is likely not what you're expecting. http://stackoverflow.com/questions/940912/linqtosql-static-datacontext-in-a-web-application – Dustin Venegas Sep 06 '13 at 08:51