Loading

Using DataContext to Track Changes and Cache Objects

The DataContext object manages object identity for you so that rows retrieved from the
database table are automatically logged in the DataContext object’s internal identity table
by the row’s primary key when the object is created. If the same row is retrieved again by this
DataContext object, the original instance is handed back to you. Your application sees only
the state of the row that was first retrieved. If, for example, you retrieve a row in your applica-
tion and the row is modified in the database table by a different application, you won’t get
the updated data if you query for this row again because the row is delivered to you from the
DataContext object’s cache.
It might seem rather weird that querying for the same row returned the original state of
the row, even though the row had been changed by another application. In essence, the new
data was thrown away. The reason for this behavior is that LINQ to SQL needs this behavior to
support optimistic concurrency. DataContext contains a method called SubmitChanges, which
sends all your changes back to the database table. This is explained in more detail later in this
lesson.

posted @ 2012-05-07 11:15  .net's  阅读(355)  评论(0编辑  收藏  举报