Share via

Entity framework code first modelling issues

vazun 21 Reputation points
2021-06-16T13:18:18.503+00:00

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.
Developer technologies | .NET | Entity Framework Core
Developer technologies | .NET | Other

Answer accepted by question author
  1. Daniel Zhang-MSFT 9,661 Reputation points
    2021-06-17T02:31:06.457+00:00

    Hi @vazun ,
    >> 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
    As shown in the error message, foreign key constraint may cause cycles or multiple cascade paths.
    From this document you can know that you receive this error message because in SQL Server, a table cannot appear more than one time in a list of all the cascading referential actions that are started by either a DELETE or an UPDATE statement. The tree of cascading referential actions must only have one path to a particular table on the cascading referential actions tree.
    And to resolve this problem, you can create a foreign key that will create a single path to a table in a list of cascading referential actions. Or you can consider using triggers as a last resort.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.