O'Reilly logo

Programming Entity Framework: DbContext by Rowan Miller, Julia Lerman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using the Change Tracker API in Application Scenarios

We’ve covered a lot of functionality in this chapter, so let’s look at a couple of examples of how that functionality can be used in an application.

You’ll see how you can use the Change Tracker API to resolve concurrency conflicts and also to log changes that are made during SaveChanges.

Resolving Concurrency Conflicts

A concurrency conflict occurs when you attempt to update a record in the database but another user has updated that same record since you queried for it. By default, Entity Framework will always update the properties that you have modified regardless of whether or not there is a concurrency conflict. However, you can configure your model so that Entity Framework will throw an exception when a concurrency conflict occurs. You do this by specifying that a specific property should be used as a concurrency token.

Note

How to configure your model for optimistic concurrency is covered in detail in Programming Entity Framework, 2e (for EDMX models) and in Programming Entity Framework: Code First (for models defined using Code First).

During SaveChanges Entity Framework will check if the value in the corresponding database column has been updated since the record was bought into memory. A concurrency exception is thrown if the value in the database has changed.

The BAGA model includes two examples of concurrency tokens. The SocialSecurityNumber property on Person is marked with the ConcurrencyCheck attribute. When updating an ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required