Entity Framework Error: Introducing the FOREIGN KEY restriction for the table can cause loops or multiple cascading paths

niklas.ullsperger
 
on Jun 16, 2021 07:38 AM
778 Views

I when I try to migrate/update my DB I always get messages, like that this and this column of this table causes multiple cascade delete paths etc. I understand what cascade delete paths are, but dont know why I get the errors in this case, because I did everything right in my option.

Since there are several models, that are connected with each other, I am giving you the link to my github repos (Folder Backend/Messanger-Backend).

https://github.com/niklasxulls/ASP-Angular-Messanger

Thank you for your help

EDIT: The error (I have also tried to comment the cause of the first error out, but there are also other errors, that appear, when the first error isnt there anymore. Thats why I have posted the repos, so you can try by yourself)

Error message in German sry

 

 PM> Add-Migration "Initial mig"
 Build started...
 Build succeeded.
 To undo this action, use Remove-Migration.
 PM> Update-Database
 Build started...
 Build succeeded.
 fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
       Failed executing DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
       CREATE TABLE [GroupMessageStatuses] (
           [GroupMessageStatusID] int NOT NULL IDENTITY,
           [GroupMessageID] int NOT NULL,
           [UserID] int NOT NULL,
           [MessageStatusID] int NOT NULL,
           [CreatedAt] datetime2 NULL,
           [UpdatedAt] datetime2 NULL,
           CONSTRAINT [PK_GroupMessageStatuses] PRIMARY KEY ([GroupMessageStatusID]),
           CONSTRAINT [FK_GroupMessageStatuses_GroupMessages_GroupMessageID] FOREIGN KEY ([GroupMessageID]) REFERENCES [GroupMessages] ([GroupMessageID]) ON DELETE CASCADE,
           CONSTRAINT [FK_GroupMessageStatuses_MessageStatuses_MessageStatusID] FOREIGN KEY ([MessageStatusID]) REFERENCES [MessageStatuses] ([MessageStatusID]) ON DELETE CASCADE,
           CONSTRAINT [FK_GroupMessageStatuses_Users_UserID] FOREIGN KEY ([UserID]) REFERENCES [Users] ([UserID]) ON DELETE CASCADE
       );
 Failed executing DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
 CREATE TABLE [GroupMessageStatuses] (
     [GroupMessageStatusID] int NOT NULL IDENTITY,
     [GroupMessageID] int NOT NULL,
     [UserID] int NOT NULL,
     [MessageStatusID] int NOT NULL,
     [CreatedAt] datetime2 NULL,
     [UpdatedAt] datetime2 NULL,
     CONSTRAINT [PK_GroupMessageStatuses] PRIMARY KEY ([GroupMessageStatusID]),
     CONSTRAINT [FK_GroupMessageStatuses_GroupMessages_GroupMessageID] FOREIGN KEY ([GroupMessageID]) REFERENCES [GroupMessages] ([GroupMessageID]) ON DELETE CASCADE,
     CONSTRAINT [FK_GroupMessageStatuses_MessageStatuses_MessageStatusID] FOREIGN KEY ([MessageStatusID]) REFERENCES [MessageStatuses] ([MessageStatusID]) ON DELETE CASCADE,
     CONSTRAINT [FK_GroupMessageStatuses_Users_UserID] FOREIGN KEY ([UserID]) REFERENCES [Users] ([UserID]) ON DELETE CASCADE
 );
 Microsoft.Data.SqlClient.SqlException (0x80131904): Das Einführen der FOREIGN KEY-Einschränkung "FK_GroupMessageStatuses_Users_UserID" für die GroupMessageStatuses-Tabelle kann Schleifen oder mehrere Kaskadepfade verursachen. Geben Sie ON DELETE NO ACTION oder ON UPDATE NO ACTION an, oder ändern Sie andere FOREIGN KEY-Einschränkungen.
 Die Einschränkung oder der Index konnte nicht erstellt werden. Siehe vorherige Fehler.
    at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
    at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
    at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
    at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
    at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite)
    at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName)
    at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()
    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
    at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
    at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
    at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
    at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
 ClientConnectionId:86f8c011-ee35-44cd-a526-a5cfe9d579be
 Error Number:1785,State:0,Class:16
 Das Einführen der FOREIGN KEY-Einschränkung "FK_GroupMessageStatuses_Users_UserID" für die GroupMessageStatuses-Tabelle kann Schleifen oder mehrere Kaskadepfade verursachen. Geben Sie ON DELETE NO ACTION oder ON UPDATE NO ACTION an, oder ändern Sie andere FOREIGN KEY-Einschränkungen.
 Die Einschränkung oder der Index konnte nicht erstellt werden. Siehe vorherige Fehler.

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download