Entity framework AddOrUpdate key violation
I am using a method AddOrUpdate
from EntityFramework 6.0
which doc should execute:
AddOrUpdate (IDbSet, TEntity [])
Adds or updates objects by key when SaveChanges is called. Equivalent to the "upsert" operation from the database. This method can be useful when seeding data using migration.
I want to do a "bulk upsert", I have a trigger that nightly transfers data from another server and inserts new values ββand existing updates. The code I'm using:
private void InsertConfigurations(IEnumerable<Configuration> configs)
{
using (var context = new MyEntities())
{
using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
{
context.Configuration.AutoDetectChangesEnabled = false;
context.Configurations.AddOrUpdate(configs.ToArray());
context.SaveChanges();
scope.Complete();
}
}
}
Now this code fails every time:
System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating records. See Inner Exception for details. ---> System.Data.Entity.Core.UpdateException: An error occurred while updating records. See Inner Exception for details. ---> System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint "PK_Configuration_ID". Unable to insert duplicate key into 'dbo.Configuration' object. The duplicate key value is (218383).
From what I understand from UPSERT
, shouldn't there be a key reason to use it to avoid the exact problem I am facing? Am I doing something wrong?
StackTrace:
--- End of inner exception stack trace ---
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
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.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
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.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
--- End of inner exception stack trace ---
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at D.DataService.DataService.InsertConfigurations(IEnumerable`1 configs) in c:\localdev\DWeb\DWeb\D.DataService\DataService.cs:line 41
at D.DataService.DataService.UpdateAllConfigurations() in c:\localdev\DWeb\DWeb\D.DataService\DataService.cs:line 27
at D.DataService.Job.ConfigurationUpdaterJob.Execute(IJobExecutionContext context) in c:\localdev\DWeb\DWeb\D.DataService\Job\ConfigurationUpdaterJob.cs:line 14
source to share
No one has answered this question yet
See similar questions:
or similar: