简体   繁体   English

错误:更新数据库命令后,表'Table_name'已存在

[英]Error: Table 'Table_name' already exists after Update-Database command

Following the model-first approach, I edited my Student-model (added a [Key] annotation). 按照模型优先的方法,我编辑了学生模型(添加了[Key]批注)。 I then removed the migration and added a new migration using 'Add-Migration Initial' which worked fine. 然后,我删除了迁移,并使用“ Add-Migration Initial”添加了新的迁移,效果很好。 After that I tried and entered the command 'Update-Database' which caused the error 'Table 'Table_name' already exists' 之后,我尝试并输入命令“ Update-Database”,这导致错误“ Table'Table_name'已经存在”

Complete stack-trace: 完整的堆栈跟踪:

 The EF Core tools version '2.1.1-rtm-30846' is older than that of the runtime '2.2.4-servicing-10062'. Update the tools for the latest features and bug fixes.
    Microsoft.EntityFrameworkCore.Infrastructure[10403]
          Entity Framework Core 2.2.4-servicing-10062 initialized 'MariaDBTestContext' using provider 'Pomelo.EntityFrameworkCore.MySql' with options: None
    infoverbose: Executed DbCommand (33ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    : Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (33ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='nowak01' AND TABLE_NAME='__EFMigrationsHistory';
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='nowak01' AND TABLE_NAME='__EFMigrationsHistory';
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT `MigrationId`, `ProductVersion`
          FROM `__EFMigrationsHistory`
          ORDER BY `MigrationId`;
    Microsoft.EntityFrameworkCore.Migrations[20402]
          Applying migration '20190502141537_Initial'.
    Applying migration '20190502141537_Initial'.
    fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
          Failed executing DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE TABLE `Student` (
              `ID` int NOT NULL AUTO_INCREMENT,
              `FirstName` longtext NULL,
              `LastName` longtext NULL,
              CONSTRAINT `PK_Student` PRIMARY KEY (`ID`)
          );
    MySql.Data.MySqlClient.MySqlException (0x80004005): Table 'Student' already exists ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Table 'Student' already exists
       at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ResultSet.cs:line 42
       at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 80
       at MySql.Data.MySqlClient.MySqlDataReader.ReadFirstResultSetAsync(IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 302
       at MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(MySqlCommand command, CommandBehavior behavior, ResultSetProtocol resultSetProtocol, IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 287
       at MySqlConnector.Core.TextCommandExecutor.ExecuteReaderAsync(String commandText, MySqlParameterCollection parameterCollection, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 37
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 261
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 62
       at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
    Failed executing DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    CREATE TABLE `Student` (
        `ID` int NOT NULL AUTO_INCREMENT,
        `FirstName` longtext NULL,
        `LastName` longtext NULL,
        CONSTRAINT `PK_Student` PRIMARY KEY (`ID`)
    );
    MySql.Data.MySqlClient.MySqlException (0x80004005): Table 'Student' already exists ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Table 'Student' already exists
       at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ResultSet.cs:line 42
       at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 80
       at MySql.Data.MySqlClient.MySqlDataReader.ReadFirstResultSetAsync(IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 302
       at MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(MySqlCommand command, CommandBehavior behavior, ResultSetProtocol resultSetProtocol, IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 287
       at MySqlConnector.Core.TextCommandExecutor.ExecuteReaderAsync(String commandText, MySqlParameterCollection parameterCollection, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 37
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 261
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 62
       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)
    Table 'Student' already exists

By removing your previous migration that creates the table 'Student' EntityFramework now is generating the migration with a Create Table Script, if you want to remove a migration you must revert it from your database aswell. 通过删除以前创建表'Student'的迁移,EntityFramework现在使用创建表脚本来生成迁移,如果要删除迁移,则还必须从数据库中还原它。 Now you can either recreate your database or revert/delete the 'Students' table on your database. 现在,您可以重新创建数据库或还原/删除数据库上的“ Students”表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 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 实体框架6中“更新数据库”之后发生错误 - Error after 'update-database' in Entity Framework 6 table_name附近的语法错误 - Syntax error near table_name EF-Core:表“名称”已存在 - 尝试更新数据库时 - EF-Core: Table "name" already exists - when trying to update database 为MySql数据库运行更新数据库时出错:表&#39;xxx&#39;不存在 - Getting error when running update-database for MySql database: Table 'xxx' doesn't exist 初始迁移后,EF6中的update-database命令不起作用 - update-database command in EF6 not working after initial migration 指定的表不存在。 [table_Name]错误 - The specified table does not exist. [ table_Name] error 使用更新数据库命令MVC 4时出错 - Error when using Update-Database command MVC 4 代码首次迁移中更新数据库命令出错 - Error in update-database command in code first migration 更新数据库代码首先迁移 ASP.NET MVC 时,ORA-00955 名称已被现有对象错误 - ORA-00955 name is already used by an existing object Error when Update-Database Code First Migration ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM