c# - MySql 数据库中的 EF 重命名列

标签 c# mysql entity-framework

我正在尝试通过 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): Parameter '@columnType' must be defined.

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 复制并粘贴到数据库中,它按预期执行而没有错误,并成功地使用正确的 int(11) 类型重命名了该列。

那么,即使它正在生成(显然)有效的 sql,我该如何防止抛出此错误?


编辑:要注意,即使我没有使用 dotconnect,这里接受的答案对我也有效。

最佳答案

找到上述问题的解决方案:(MySql.Data.MySqlClient.MySqlException):据我所知,它与连接器的升级有关。

您需要在连接字符串中添加 Allow User Variables=True 才能使用自定义变量。

点击此链接供您引用:

http://blog.tjitjing.com/index.php/2009/05/mysqldatamysqlclientmysqlexception-parameter-id-must-be-defined.html

关于c# - MySql 数据库中的 EF 重命名列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49080607/

相关文章:

java - Spring Jdbc 模板返回 Int 为 Long?

c# - 如果未进行任何更改, Entity Framework DbContext 是否会保存更改?

c# - 疯狂 VS 设计器错误

C# MySql 连接。如何传递图像?

c# - 如何解决使用主成分分析引发的 OutOfMemoryException

c# - viewstate MAC 验证失败 - 查看状态错误

javascript - angular.js 使用来自 mysql 数据库的数据

c# - 通过 where 子句传入 func

mysql - SQL中UNION之后的WHERE语句?

c# - 在删除属性之前,必须删除或重新定义所有包含的外键 - EF Core