Entity Framework Core 2.2.4 Duplicate insertion DbUpdateException deleted the original record. Is this even possible?


When a duplicate insert exception occurs, I found the original row got deleted when the illegal insertion tried to occur due to some bug in our code. Is this happening because of using the DeleteBehavior.ClientSetNull action? What am I missing here? Nowhere in the code are we deleting records - no triggers, not even soft deletes.

Code below showing the exception thrown and EF Core generated DbContext entity.


public async Task<int> AddCampaign(DigitalCampaign campaign)
        await _dbContext.DigitalCampaign.AddAsync(campaign);
        return await _dbContext.SaveChangesAsync();
    catch (DbUpdateException ex)
        throw new ApplicationException(
            $"There was DbUpdateException error saving the Campaign {campaign.DcId} in OMS database. O1 SalesOrder: {campaign.OperativeSalesOrder.Single().SalesOrderId.ToString()}." +
            $"{Environment.NewLine} Error: {ex.InnerException}");
    catch (Exception e)
        throw new ApplicationException(
            $"There was an error saving the Campaign {campaign.DcId} in OMS database. O1 SalesOrder: {campaign.OperativeSalesOrder.Single().SalesOrderId.ToString()}." +
            $"{Environment.NewLine} Error: {e.InnerException}");

Affected Entity:

entity.HasOne(d => d.DigitalCampaign)
.WithMany(p => p.CampaignOperativeProduct)
.HasForeignKey(d => d.DigitalCampaignId)
.HasConstraintName("CampaignOperativeProduct DigitalCampaignId fk");


There was DbUpdateException error saving the Campaign a088A000002CWrIQAW in OMS database. O1 SalesOrder: 30002231.
Error: System.Data.SqlClient.SqlException (0x80131904): Cannot insert duplicate key row in object 'OMS.CampaignOperativeProduct' with unique index 'DigitalCampaignId and OperativeProductId together must be unique'. The duplicate key value is (2325, 11).
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows)
at System.Data.SqlClient.SqlDataReader.TryHasMoreResults(Boolean& moreResults)
at System.Data.SqlClient.SqlDataReader.TryNextResult(Boolean& more)
at System.Data.SqlClient.SqlDataReader.<>c__DisplayClass187_0.b__1(Task t)
at System.Data.SqlClient.SqlDataReader.InvokeRetryable[T](Func2 moreFunc, TaskCompletionSource1 source, IDisposable objectToDispose)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken) ClientConnectionId:d3869a6f-d7ba-4ed4-bae7-1e5a2c3f2e6a
Error Number:2601,State:1,Class:14
asked on Stack Overflow Oct 11, 2019 by Ashish Yengkhom • edited Oct 11, 2019 by Ashish Yengkhom

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0