OptimisticConcurrency Exception when no changes made

c# entity-framework entity-framework-6 optimistic-concurrency


Occasionally, I receiveOptimisticConcurrency Calling exceptionsSaveChanges() Despite the fact that I didn't make any changes to any objects throughout this thread of execution, I was still using Entity Framework 6 within an MVC web application. It's possible that modifications were done in a different thread or process. It appears impossible for aOptimisticConcurrency Sadly, it happens occasionally even though it should be the exception.

var launchedSurvey = DB.Find<LaunchedSurvey>(id);
DB.SaveChanges(); // throws OptimisticConcurrencyException 

I callSaveChanges simply as a test since I was getting the same problem when records were added but no changes to existing objects occurred, which also seems should not throw an exception, even though no changes occurred.OptimisticConcurrencyException .

The error message appears here.

System.Exception: TestSave Failed ---> System.Data.Entity.Infrastructure.DbUpdateException: SaveChangesAndReload caught exception and attempted to handle it => attempts:5
 ---> System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details. ---> System.Data.Entity.Core.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ValidateRowsAffected(Int64 rowsAffected, UpdateCommand source)
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
   at System.Data.Entity.Internal.InternalContext.SaveChanges()

The LaunchedSurvey C-Tor is available here.

public LaunchedSurvey()
    CreationTime = DateTime.UtcNow;
    WorkerLaunchedSurveys = new Collection<WorkerLaunchedSurvey>();
    WorkerQualificationRequests = new Collection<WorkerQualificationRequest>();
    _remainingDemographic = new DateTime(1900, 1, 1);
    MinimumPercent = 0.10;
    ExcludeList = new List<LaunchedSurvey>();
    IncludeList = new List<LaunchedSurvey>();
    ScreenedWorkers = new List<ScreenedWorker>();
    EmailMessages = new List<EmailMessage>();
    AssignmentsPendingWorkers = new HashSet<AssignmentsPendingWorker>();
    ExcludedWorkersManual = new List<AmazonWorkerExcluded>();
    IncludedWorkersManual = new List<AmazonWorkerIncluded>();
    PendingWorkersQualificationsUnknown = new List<AmazonWorkerPending>();
    Tags = new List<Tag>();
    ManualQualificationTypeIdList = new List<QualificationType>();
    WorkerSurveyStatisticAuto = new WorkerSurveyStatisticAuto();
    WorkerSurveyStatisticManual = new WorkerSurveyStatisticManual();
    WorkerUniqueIdentifierList = new List<WorkerUniqueIdentifier>();

Any knowledge on this would be highly appreciated!

8/21/2015 3:09:15 AM

Accepted Answer

5/23/2017 11:44:28 AM

Popular Answer

Be mindful that EF's optimistic concurrency handling mainly relies on the amount of affected rows that queries return in order to identify problems. Therefore, additional causes that result in an update, insert, or deletion returning an unexpected amount of impacted rows may also cause an OptimisticConcurrencyException to be issued.

In my situation, I was adding a second row to a table containingIGNORE_DUB_KEY=ON . This makes the SQL server return "0 row(s) affected" along with a warning, on primary key constraint violations, rather than throwing an error.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow