运行EF Core命令'更新数据库' In .Net Core 2.2应用程序正在应用旧迁移而不是当前迁移
我正在使用使用EF Core(首先)的.NET Core版本2.2应用程序(带有Angular)。这已经是一个现有的项目已有几年了,大约一个月前,我已经克隆了它。似乎已经应用了许多以前的迁移文件。
我对模型进行了一些更改,更新了上下文,并使用以下方式创建了一个新的迁移:
dotnet ef migrations add AddAssignmentEndDateToSMEAssignment
迁移文件没有问题,上下文快照,一切看起来都不错。我接下来要运行:
dotnet ef database update
而且我会收到以下错误:
> Applying migration '20200323181854_userProfileUpdate'.
Failed executing DbCommand (35ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [nssr_roles] (
[role_name] varchar(255) NOT NULL,
CONSTRAINT [PK_nssr_roles] PRIMARY KEY ([role_name])
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource 1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary 2 parameterValues)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary 2 parameterValues)
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 contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:ff4b615f-69a6-450e-9c8e-4f3e5de4497c
Error Number:2714,State:6,Class:16
There is already an object named 'nssr_roles' in the database.
因此,它正在尝试应用旧的迁移(20200323181854_USERPROFILEUPDATE-一个从2020年起也很少),这是一个完全不同的表(NSSR_ROLES)。我知道一个正在从事此应用程序的人大概是3周前进行了新的迁移,所以这并不是说这个迁移是在审理的。我还尝试了以下命令,同样的事情:
dotnet ef database update 20220506044703_AddAssignmentEndDateToSMEAssignment
&amp;
dotnet ef database update AddAssignmentEndDateToSMEAssignment
我尝试删除我新添加的迁移,然后一旦删除了
dotnet ef migrations remove
迁移消息说已经应用了。
这里发生了什么,我该怎么办来解决这个问题?我不想删除数据库中的任何内容,因此我不想删除迁移并从头开始。在进行迁移然后推动git时会出现问题,导致将其提出问题的人吗?
我知道它是一个旧版本,然后在路上进行更新
I'm working on a .NET Core version 2.2 app (with Angular), which uses EF Core (code first). It's been an existing project for a few years, I've cloned it about a month ago. There are lots of previous migration files all which seemingly have been applied already.
I made some changes to a model, updated the context, and created a new migration using:
dotnet ef migrations add AddAssignmentEndDateToSMEAssignment
Migration file created no problem, context snapshot and everything looks good. I run this next:
dotnet ef database update
And I get the following error:
> Applying migration '20200323181854_userProfileUpdate'.
Failed executing DbCommand (35ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [nssr_roles] (
[role_name] varchar(255) NOT NULL,
CONSTRAINT [PK_nssr_roles] PRIMARY KEY ([role_name])
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource 1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary 2 parameterValues)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary 2 parameterValues)
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 contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:ff4b615f-69a6-450e-9c8e-4f3e5de4497c
Error Number:2714,State:6,Class:16
There is already an object named 'nssr_roles' in the database.
So it's trying to apply an old migration (20200323181854_userProfileUpdate - one from 2020 no less) which is dealing with a completely different table (nssr_roles). I know someone who was working on this app made a new migration maybe 3 weeks ago so it's not that this one was pending. I also tried the below commands, same thing:
dotnet ef database update 20220506044703_AddAssignmentEndDateToSMEAssignment
&
dotnet ef database update AddAssignmentEndDateToSMEAssignment
I tried removing my newly added migration, then once it was removed I ran the remove again to see if there was anything pending:
dotnet ef migrations remove
But there wasn't, it showed the latest applied migration (the one my colleague did 3 weeks ago) with a message saying it was already applied.
What is happening here and what can I do to fix this? I don't want to delete anything in the database so I don't want to delete migrations and start from scratch. Can something go wrong when doing migrations and then pushing to git, causing whoever pulls it to have issues?
I know its an old version by the way, an update should happen down the road
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,我了解到我们的团队做得不同:
与其运行该更新命令:
因此,它不会像更新命令那样自动更新数据库,但是它会在确认脚本做应该执行的操作后在数据库中输出一个SQL脚本。这样做似乎不太直观,但是如果您有很多外国钥匙等,显然它会增加一些安全性。
仍然不确定为什么要出现的迁移(旧的)不在历史表中,但是这种方法奏效了。
So I learned our team does it differently:
Instead of running that update command they do:
so it doesn't automatically update the database like the update command would, but it outputs you an SQL script that you run within your database once you confirm the script is doing what it should be doing. It seems less intuitive to do it this way but apparently it adds a bit of safety if you have a lot of foreign keys etc.
Still not sure why the migration in question that was coming up (old one) wasn't in the history table, but this approach worked.