微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

实体框架代码优先建模问题

如何解决实体框架代码优先建模问题

当我尝试迁移/更新我的数据库时,我总是收到消息,就像此表的这一列和此列导致多个级联删除路径等。我了解级联删除路径是什么,但不知道为什么会出现错误在这种情况下,因为我在我的选择中做的一切都是正确的。

由于有几个模型相互连接,我给你我的 github 存储库的链接文件夹后端/Messanger-Backend)。

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

感谢您的帮助

编辑: 错误(我也尝试将第一个错误的原因注释掉,但是当第一个错误不再存在时,还会出现其他错误。这就是我发布 repos 的原因,因此您可以自己尝试)

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=[],CommandTimeout='30']
CREATE TABLE [GroupMessageStatuses] (
    [GroupMessageStatusID] int NOT NULL IDENTITY,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,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,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 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.

解决方法

我自己解决了这个问题。我不得不重新排列整个数据库,但现在它可以工作了。我有问题,比如双重关系。我还修复了 onDelete 问题,在 SQL Server 中创建了一个 ER 图表,这让我更容易找到我的错误。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。