实体框架代码优先建模问题
Posted
技术标签:
【中文标题】实体框架代码优先建模问题【英文标题】:Entity framework code first modelling issues 【发布时间】:2021-09-01 01:37:11 【问题描述】:当我尝试迁移/更新我的数据库时,我总是收到消息,例如此表和此表的此列会导致多个级联删除路径等。我了解级联删除路径是什么,但不知道为什么会出现错误在这种情况下,因为我在我的选择中做的一切都是正确的。
由于有多个模型相互连接,因此我为您提供了我的 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=[], 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.
【问题讨论】:
能否提供原错误信息? 可能,你有 entity1 引用 entity2 和 entity2 也引用 entity1。 嗯,快速浏览一下,首先注意到GroupMessage
与UserMessage
有完全相同的问题,因此需要类似的解决方案。
但是GroupMessage和UserMessage的区别在于,UserMessage引用了2次User Model,而GroupMessage只引用了一次User和Group。
能否给我们您使用的表格并告诉我们这些表格之间的关系?您提供的链接找不到任何代码。
【参考方案1】:
我自己解决了这个问题。我不得不重新设置整个数据库,但现在它可以工作了。我有问题,比如双重关系。我还修复了 onDelete 问题,在 SQL Server 中创建了一个 ER 图,这让我更容易找到我的错误。
【讨论】:
以上是关于实体框架代码优先建模问题的主要内容,如果未能解决你的问题,请参考以下文章