Entity framework 代码优先建模问题
Entity framework code first modelling issues
当我尝试 migrate/update 我的数据库时,我总是收到消息,比如这个和这个 table 的这一列导致多个级联删除路径等。我了解什么是级联删除路径,但不知道为什么我在这种情况下会收到错误,因为我在我的选项中做的一切都是正确的。
由于有多个模型,它们相互关联,我将 link 交给我的 github 回购(文件夹 Backend/Messanger-Backend)。
https://github.com/niklasxulls/ASP-Angular-Messanger
感谢您的帮助
编辑:
错误(我也尝试将第一个错误的原因注释掉,但是当第一个错误不再存在时,还会出现其他错误。这就是我发布回购协议的原因,因此您可以自己尝试)
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.
我自己解决了这个问题。我不得不重新定义整个数据库,但现在可以了。我有问题,比如双重关系。我还修复了 onDelete 问题,方法是在 SQL 服务器中创建一个 ER 图,这让我更容易找到我的错误。
当我尝试 migrate/update 我的数据库时,我总是收到消息,比如这个和这个 table 的这一列导致多个级联删除路径等。我了解什么是级联删除路径,但不知道为什么我在这种情况下会收到错误,因为我在我的选项中做的一切都是正确的。
由于有多个模型,它们相互关联,我将 link 交给我的 github 回购(文件夹 Backend/Messanger-Backend)。
https://github.com/niklasxulls/ASP-Angular-Messanger
感谢您的帮助
编辑: 错误(我也尝试将第一个错误的原因注释掉,但是当第一个错误不再存在时,还会出现其他错误。这就是我发布回购协议的原因,因此您可以自己尝试)
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.
我自己解决了这个问题。我不得不重新定义整个数据库,但现在可以了。我有问题,比如双重关系。我还修复了 onDelete 问题,方法是在 SQL 服务器中创建一个 ER 图,这让我更容易找到我的错误。