Update-Database命令在ASP.Net Core/Entity Framework Core中不起作用,因为数据库中的对象已存在 [英] Update-Database command is not working in ASP.Net Core / Entity Framework Core because object in database already exists

查看:415
本文介绍了Update-Database命令在ASP.Net Core/Entity Framework Core中不起作用,因为数据库中的对象已存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过命令行更新数据库,但是随后我手动更新了其中一个表.

I was updating my database by the command line, but then I manually updated one of my tables.

这似乎破坏了我的数据库更新能力.尝试更新时收到以下错误:

This seems to have disrupted my ability to update-database. I receive the following error when I try to update:

 System.Data.SqlClient.SqlException: There is already an object named 'ClientsAndTestimonials' in the database.    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
    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, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean openConnection, Boolean closeConnection) 
    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues, Boolean manageConnection) 
    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.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType) 
    at Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand.<>c__DisplayClass0_0.<Configure>b__0() 
    at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
 ClientConnectionId:d89989a8-ce8b-4167-be7e-fcddc4bcdf98
 Error Number:2714,State:6,Class:16
 There is already an object named 'ClientsAndTestimonials' in the database. 

过去几天,我一直在尝试解决此问题.大多数其他开发人员建议使用添加迁移重置" -IgnoreChanges的一些变体,例如以下

I have been trying to fix this problem for the past few days. Most fellow developers suggest some variation of using Add-migration "Reset" -IgnoreChanges, like John Salewski from the following link.

但是,我不断收到一条错误消息,指出找不到与参数名称'IgnoreChanges'相匹配的参数".

However, I keep getting an error that says "A parameter cannot be found that matches parameter name 'IgnoreChanges'".

任何建议将不胜感激!

Any suggestions would be greatly appreciated!

推荐答案

首先,不是EF 6,而是EF Core. EF Core当前没有-IgnoreChanges(请参见此处),但是您可以实现等效的功能通过注释掉Up()方法中的所有代码并应用迁移.这将获取当前模型状态的快照,以便后续迁移仅包括从该点开始的更改.

First, that is not EF 6, it is EF Core. There is no -IgnoreChanges currently in EF Core (see here) but you can achieve the equivalent by commenting out all the code in the Up() method and applying the migration. This will take a snapshot of the current model state so that subsequent migrations will only include changes from that point forward.

因此,如果您只是进行了一些增量模型更改,而又没有此初始基准,则可能需要删除这些更改,应用基准迁移,然后再添加您的更改并添加第二次迁移.

So if you just made some incremental model change and you don't have this initial baseline you may need to remove those changes, apply the baseline migration, then add your changes back and add a 2nd migration.

这篇关于Update-Database命令在ASP.Net Core/Entity Framework Core中不起作用,因为数据库中的对象已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆