Ошибка: таблица «Table_name» уже существует после команды Update-Database - PullRequest
0 голосов
/ 02 мая 2019

Следуя подходу, основанному на модели, я отредактировал модель Стьюдента (добавил аннотацию [Key]). Затем я удалил миграцию и добавил новую миграцию, используя Add-Migration Initial, которая работала нормально. После этого я попытался ввести команду «Обновление базы данных», которая вызвала ошибку «Таблица« Имя таблицы »уже существует»

Полная трассировка стека:

 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

1 Ответ

1 голос
/ 02 мая 2019

Удаляя предыдущую миграцию, которая создает таблицу 'Student', EntityFramework теперь генерирует миграцию с помощью скрипта Create Table. Если вы хотите удалить миграцию, вы должны также отозвать ее из своей базы данных.Теперь вы можете либо воссоздать свою базу данных, либо восстановить / удалить таблицу «Студенты» в вашей базе данных.

...