"DELETE statement conflicted with the REFERENCE constraint EF core" after SaveChanges()

0

I am using Entity Framework Core with .net core 2.2 and am encountering a strange intermittent issue.

In my case I have these three tables with entities (simplified): Data Model:

public class Case : BaseEntity
    {
        public int CaseId { get; set; }
        public Guid ClientId { get; set; }
        public virtual Client Client { get; set; }
    }
    public class Client : BaseEntity
    {
        public Guid ClientId { get; set; }
        public Guid PersonId { get; set; }
    public virtual ICollection<Case> Case { get; set; }
    }

Configuration:

modelBuilder.Entity<Case>(entity =>
            { entity.HasOne(d => d.Client).WithMany(p => p.Case).HasForeignKey(d => d.ClientId).OnDelete(DeleteBehavior.Restrict);
});

In order to add a new Case in code I am doing something like this:

1. Begin the transaction

2. Calling a SP in which I am adding a client and adding some dependenttable info and returning the ClientId from  it.

3. I am passing the client Id to next step as mentioned below:

objCase.Client=objClient;
objCase.ClientId=objClient,ClientId;
_Context.Client.Add(objCase);

4. Now I am calling the below methods which is throwing the exception
_context.SaveChanges();

So far this has worked fine and a new entry is created in the Client table with the ID and personId and a new entry is created in the Case table with an entry linking the Client to the establishment.

Suddenly I am encountering an error for one user (but not another) where the code hangs for a while and then gives the following exception:

SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_Case_Client_ClientId". The conflict occurred in database "CenterManagementSystem", table "dbo.Case", column 'ClientId'.

The statement has been terminated.

Full AppInsight:

Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.RemoteDependency","time":"2019-06-11T10:30:42.1388220Z","sampleRate":50,"tags":{"ai.application.ver":"1.0.0.0","ai.cloud.roleInstance":"ICEN083","ai.operation.id":"e18f8d7fa6aecf448350d74dea9ceedb","ai.operation.parentId":"|e18f8d7fa6aecf448350d74dea9ceedb.65995757_","ai.operation.name":"POST Clients/PostCase [clientId/orgId]","ai.location.ip":"::1","ai.internal.sdkVersion":"rdddsc:2.9.1-26132","ai.internal.nodeName":"ICEN083"},"data":{"baseType":"RemoteDependencyData","baseData":{"ver":2,"name":"servername | CenterManagementSystem","id":"0602a78dd80f4716a98e37f9fa8c9c91","data":"SET NOCOUNT ON;\r\nDELETE FROM [Client]\r\nWHERE [ClientId] = @p0;\r\nSELECT @@ROWCOUNT;","duration":"00:00:02.5031198","resultCode":"547","success":false,"type":"SQL","target":"servername | CenterManagementSystem","properties":{"AspNetCoreEnvironment":"qa","DeveloperMode":"true","_MS.ProcessedByMetricExtractors":"(Name:'Dependencies', Ver:'1.1')","Exception":"System.Data.SqlClient.SqlException (0x80131904): The DELETE statement conflicted with the REFERENCE constraint \"FK_Case_Client_ClientId\". The conflict occurred in database \"CenterManagementSystem\", table \"dbo.Case\", column 'ClientId'.\r\nThe statement has been terminated.\r\n   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()\r\n   at System.Data.SqlClient.SqlDataReader.get_MetaData()\r\n   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)\r\n   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)\r\n   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Common.DbCommand.ExecuteReader()\r\n   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)\r\nClientConnectionId:58a6c6e2-b1a8-41ea-93a3-f8fbce2035d3\r\nError Number:547,State:0,Class:16"}}}}

I guess my main question is why is entity framework trying to delete something in the process of doing an insert?

Any insight, help or guidance would be very much appreciated.

c#
asp.net-core
entity-framework-core
asked on Stack Overflow Jun 11, 2019 by Ram kishan • edited Jun 12, 2019 by Ram kishan

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0