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

0

Following the model-first approach, I edited my Student-model (added a [Key] annotation). I then removed the migration and added a new migration using 'Add-Migration Initial' which worked fine. After that I tried and entered the command 'Update-Database' which caused the error 'Table 'Table_name' already exists'

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
c#
visual-studio
entity-framework
entity-framework-core
asked on Stack Overflow May 2, 2019 by Jimmy

1 Answer

1

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. Now you can either recreate your database or revert/delete the 'Students' table on your database.

answered on Stack Overflow May 2, 2019 by Tiago Silva

User contributions licensed under CC BY-SA 3.0