Я пытаюсь переименовать столбец через миграцию в моей базе данных MySql.
Это моя миграция:
public override void Up()
{
//RenameColumn("Docks", "ProfileId", "SecondId"); <!-- doesn't work either
RenameColumn("Docks", "ProfileId", "SecondId", anonymousArguments: new { ColumnType = "int" });
}
public override void Down()
{
//RenameColumn("Docks", "SecondId", "ProfileId"); <!-- doesn't work either
RenameColumn("Docks", "SecondId", "ProfileId", anonymousArguments: new { ColumnType = "int" });
}
Когда я запускаю Update-Database
он выводит на меня большую ошибку (если вы хотите увидеть всю ошибку, откройте фрагмент), который включает в себя:
MySql.Data.MySqlClient.MySqlException(0x80004005): Параметр '@columnType' должен быть определен.
MySql.Data.MySqlClient.MySqlException(0x80004005): Fatal error encountered during command execution.-- - > MySql.Data.MySqlClient.MySqlException(0x80004005): Parameter '@columnType' must be defined. at MySql.Data.MySqlClient.Statement.SerializeParameter(MySqlParameterCollection
parameters, MySqlPacket packet, String parmName, Int32 parameterIndex) at MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql, MySqlParameterCollection parameters, MySqlPacket packet) at MySql.Data.MySqlClient.Statement.BindParameters()
at MySql.Data.MySqlClient.PreparableStatement.Execute() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.
< NonQuery> b__0(DbCommand t, DbCommandInterceptionContext '1 c) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher' 1. Dispatch[TTarget, TInterceptionContext, TResult](TTarget target, Func '3 operation, TInterceptionContext interceptionContext,
Action' 3 executing, Action '3 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(MigrationStatement
migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable' 1 migrationStatements, DbConnection connection, DbTransaction
transaction, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable '1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinNewTransaction(IEnumerable'
1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable '1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable' 1 migrationStatements, DbConnection connection) at System.Data.Entity.Migrations.DbMigrator.
<> c__DisplayClass32.
< ExecuteStatements> b__30() at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute(Action operation) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable '1 migrationStatements, DbTransaction existingTransaction) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable'
1 migrationStatements) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable '1 migrationStatements) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel,
IEnumerable' 1 operations, IEnumerable '1 systemOperations, Boolean downgrading, Boolean auto) at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration
migration, DbMigration lastMigration) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable' 1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable
'1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) at System.Data.Entity.Migrations.DbMigrator.
<>c__DisplayClasse.
<Update>b__d() at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.DbMigrator.Update(String
targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Fatal error encountered during command execution.
Подробный режим сгенерировал следующий SQL-запрос до того, как он сбросил ошибку:
set @columnType := (select case lower(IS_NULLABLE)
when 'no' then CONCAT(column_type, ' not null ')
when 'yes' then column_type end
from information_schema.columns
where table_name = 'Docks'
and column_name = 'ProfileId');
set @sqlstmt := (select concat('alter table 'Docks'
change 'ProfileId' 'SecondId' ' , @columnType));
prepare stmt from @sqlstmt;
execute stmt;
deallocate prepare stmt;
Мне было любопытно, поэтому я копировал и вставлял sql в db, и он выполнялся как ожидалось без ошибок и успешно переименовал столбец с правильным типом int(11)
.
Итак, как я могу предотвратить эту ошибку, даже если она генерирует (по-видимому) действительный sql?
Edit: Хотел отметить, что принятый ответ здесь работал для меня, хотя я не использовал dotconnect.
Найденное решение вышеупомянутой проблемы: (MySql.Data.MySqlClient.MySqlException): насколько я понимаю, это связано с обновлением соединителя.
Чтобы использовать пользовательские переменные, в строке подключения необходимо добавить параметр Allow User Variables=True
.
Перейдите по этой ссылке для справки:
Когда ALTERing
столбца, вы должны указать полное определение, а не только одно, что вы хотите изменить. Вы упустили тип данных (INT) и, возможно, другие вещи.
Я предлагаю вам получить результат для SHOW CREATE TABLE
, изменить одну строку, о которой идет речь, а затем использовать ее в ALTER
. Несмотря на беспорядок, это, вероятно, еще проще, чем попытка восстановить полное описание рассматриваемого поля.
На практике проще выполнять задачу вручную, а не программно.
Вверх и вниз используются те же типы столбцов. Я предполагаю, что вы хотите изменить только имя столбца. Интересно, что вы можете попробовать без анонимных аргументов:
public override void Up()
{
// RenameColumn("Docks", "ProfileId", "SecondId"); // old answer
RenameColumn("Docks", "ProfileId", "SecondId", anonymousArguments: new { columnType = "int" });
}
public override void Down()
{
// RenameColumn("Docks", "SecondId", "ProfileId"); // old answer
RenameColumn("Docks", "SecondId", "ProfileId", anonymousArguments: new { columnType = "int" });
}