Database Reference
In-Depth Information
using (var context = new Recipe5Context())
{
var watch = new Stopwatch();
watch.Start();
var accounts = context.Accounts.Include("Payments").ToList();
watch.Stop();
Console.WriteLine("Time to read: {0} seconds", watch.Elapsed.TotalSeconds.ToString());
watch.Restart();
foreach (var account in accounts)
{
account.Balance += 10M;
account.Payments.First().Paid += 1M;
}
context.SaveChanges();
watch.Stop();
Console.WriteLine("Time to update: {0} seconds", watch.Elapsed.TotalSeconds.ToString());
}
How It Works
With later versions of Entity Framework, including version 6.0, we specificy POCO classes to represent entities. POCO
is an abbreviation for a Plain Old CLR Object , which is a class that typically contains only states, or properties, that
map to corresponding database columns. POCO classes have no dependencies beyond the .NET CLR base classes
and, specifically, no dependency on Entity Framework.
Change tracking with POCO entity classes occurs using either snapshots or proxy classes. With the snapshot
approach, Entity Framework takes a picture, so to speak, of the data values of an entity as it is loaded into the context
object from a query or an Attach() operation. Upon a SaveChanges() operation, the original snapshot is compared to the
current data values to determine the data values that have changed. Using this approach, Entity Framework maintains
two copies of each object and compares them, generating the necessary corresponding SQL Update, Insert, and Delete
statements. You might expect this approach to be very slow, but Entity Framework is very fast in finding the differences.
the Add() operation from the context object does not invoke a snapshot as the entity is new and there is no
need to track changes to the individual values. entity framework marks the entity as added , and it will issue a SQL Insert
statement upon a SaveChanges() operation.
Note
The second approach, depicted in Listing 13-19, wraps the underlying entity POCO object with a proxy
object that implements the IEntityWithChangeTracking interface. This proxy is responsible for notifying the
Object State Manager of changes to values and relationships on the object. Entity Framework automatically
creates these proxies for your POCO object when you mark all of the properties on your POCO class as virtual
and mark all Navigation properties that return a collection as ICollection<T> . Proxies avoid the potentially
complex object-by-object comparisons of the snapshot approach. It does, however, require some overhead to
track each change as it occurs.
Although change-tracking proxies immediately notify the change tracker components about changes to the
objects and avoid object comparisons, in practice, performance benefits are typically seen only when the model is
quite complex and/or when few changes are made to a large number of objects. The model in Figure 13-7 is very
simple, and every object is updated in the code in Listing 13-19. If you were to change the code to use snapshots, you
would notice only a second or so saved for the updates when proxies are used.
 
 
Search WWH ::




Custom Search