TechNet
Products
IT Resources
Downloads
Training
Support
Products
Windows
Windows Server
System Center
Microsoft Edge
Office
Office 365
Exchange Server
SQL Server
SharePoint Products
Skype for Business
See all products »
Resources
Channel 9 Video
Evaluation Center
Learning Resources
Microsoft Tech Companion App
Microsoft Technical Communities
Microsoft Virtual Academy
Script Center
Server and Tools Blogs
TechNet Blogs
TechNet Flash Newsletter
TechNet Gallery
TechNet Library
TechNet Magazine
TechNet Wiki
Windows Sysinternals
Virtual Labs
Solutions
Networking
Cloud and Datacenter
Security
Virtualization
Updates
Service Packs
Security Bulletins
Windows Update
Trials
Windows Server 2016
System Center 2016
Windows 10 Enterprise
SQL Server 2016
See all trials »
Related Sites
Microsoft Download Center
Microsoft Evaluation Center
Drivers
Windows Sysinternals
TechNet Gallery
Training
Expert-led, virtual classes
Training Catalog
Class Locator
Microsoft Virtual Academy
Free Windows Server 2012 courses
Free Windows 8 courses
SQL Server training
Microsoft Official Courses On-Demand
Certifications
Certification overview
Special offers
MCSE Cloud Platform and Infrastructure
MCSE: Mobility
MCSE: Data Management and Analytics
MCSE Productivity
Other resources
Microsoft Events
Exam Replay
Born To Learn blog
Find technical communities in your area
Azure training
Official Practice Tests
Support options
For business
For developers
For IT professionals
For technical support
Support offerings
More support
Microsoft Premier Online
TechNet Forums
MSDN Forums
Security Bulletins & Advisories
Not an IT pro?
Microsoft Customer Support
Microsoft Community Forums
Sign in
Home
Library
Wiki
Learn
Gallery
Downloads
Support
Forums
Blogs
Resources For IT Professionals
United States (English)
Россия (Pусский)
中国(简体中文)
Brasil (Português)
Skip to locale bar
Post an article
Translate this page
Powered by
Microsoft® Translator
Wikis - Page Details
First published by
Rick Saling - MSFT
When:
18 Jul 2011 12:06 PM
Last revision by
Ed Price - MSFT
(Microsoft)
When:
15 Feb 2013 8:16 PM
Revisions:
8
Comments:
2
Options
Subscribe to Article (RSS)
Share this
Engage!
Wiki Ninjas Blog
(
Announcements
)
Wiki Ninjas on Twitter
TechNet Wiki Discussion Forum
Can You Improve This Article?
Positively!
Click Sign In to add the tip, solution, correction or comment that will help other users.
Report inappropriate content using
these instructions
.
Wiki
>
TechNet Articles
>
Entity Framework FAQ: Concurrency
Entity Framework FAQ: Concurrency
Article
History
Entity Framework FAQ: Concurrency
Table of Contents
How do I properly handle concurrency
How does concurrency work when there is inheritance and the concurrency column is on the parent table, while the child table's properties were modified
Back to EF FAQs Table of Contents
How do I properly handle concurrency
The
Entity Framework
has built-in support for optimistic concurrency. This means that if you properly attribute your entity property metadata to indicate which property should act as the concurrency token (meaning whenever a part of the entity changes this property will also change--it could be a version number or timestamp or something), when you change an entity's properties and then call SaveChanges on the ObjectContext, the EF will check to make sure that the concurrency token has not changed since you last retrieved the entity.
When you call SaveChanges, all operations occur within a database transaction, so if a concurrency token change or other problem occurs, the transaction will be rolled back, the objects will remain in the context in basically the same state they were in when you called SaveChanges and an exception will be thrown. This allows you to make changes to the conflicting objects so the save can be attempted again. You can use the Refresh method to update the objects, or execute a query with either MergeOption PreserveChanges or OverwriteChanges, depending on what you want do to with the changes you have made on the client. These types of queries are essentially what Refresh does, but Refresh is a bit simpler--particularly when you need to update specific individual objects. More on concurrency here:
Saving changes and managing concurrency
The following blog posts talk about concurrency and model first:
D3 fun with concurrency and model first part-1
D3 fun with concurrency and model first part-2
The following blog posts talk about managing optimistic concurrency updates in n-tier applications:
Optimistic concurrency updates using entity framework in n-tier and n-layer applications part 2
Better n-tier concurrency management for the entity framework
The following tutorials explain how to handle concurrency in ASP.NET web applications:
Handling Concurrency with the Entity Framework in an ASP.NET Web Application
(Database First, Web Forms)
Handling Concurrency with the Entity Framework in an ASP.NET MVC Application
(Code First, MVC)
Additional resources:
Post on Mike Taulty's blog
Entity Framework concurrency mode and fun observations
How does concurrency work when there is inheritance and the concurrency column is on the parent table, while the child table's properties were modified
Whenever any part of an entity that has a concurrency token is modified, the update system of the Entity Framework guarantees that the table with the concurrency value is "touched". In cases where there isn't a real modification being made, a fake update statement is issued that doesn't change anything but does force the rowversion to update.
Back to EF FAQs Table of Contents