Seems like EFCore is unable to manage 2 composite FK to the same PK (Self relationship)
System.Data.SqlClient.SqlException (0x80131904): The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Objects_Container_ContainerId". The conflict occurred in database "testApp", table "dbo.Container", column 'ContainerId'.
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 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.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
ClientConnectionId:0d3ffd7e-b924-4e41-9d34-c6826a499fd0
Error Number:547,State:0,Class:16
We reproduce the case in this console app.
https://github.com/Samoulou/CompositeKey_EFCore_Issue
Four cases are describe. All four should work, but the case 3 and 4 generate FK constraint error
EF Core version: 2.1.4
Operating system: Windows 10
IDE: Visual Studio 2017 15.8.7
Note for triage: also repros on 2.2 preview3
@ajcvickers poaching
@AndriySvyryd poached to you
When fixing up a dependent that has the navigation set to an untracked principal and the FK set that doesn't match any tracked entity we should propagate the FK value to that principal when we start tracking it.
Most helpful comment
Note for triage: also repros on 2.2 preview3