使用EF 5 Code First Migration的MySql:更改表列名称时出错(Fluent API)

时间:2013-01-04 18:32:15

标签: mysql ef-code-first entity-framework-5

我正在使用EF5 CodeFirst和MySql DB。

我有一个班级'用户'

public class User
{
    [Key]
    public string Username { get; set; }
    public string DisplayName { get; set; }
}

我想将表格列 DisplayName 更改为 display_name

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
            .Property(u => u.DisplayName)
            .HasColumnName("display_name");
    }

然后我执行了Add-Migration ChangeDisplayNameUpdate-Database -Verbose并收到错误。

Using StartUp project 'CodeFirstNewDatabaseSample'.
Using NuGet project 'CodeFirstNewDatabaseSample'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'CodeFirstNewDatabaseSample' (DataSource: localhost, Provider: MySql.Data.MySqlClient, Origin: Configuration).
Applying code-based migrations: [201301041748445_ChangeDisplayName].
Applying code-based migration: 201301041748445_ChangeDisplayName.
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 = `Users` and column_name = `DisplayName` );
set @sqlstmt := (select concat(`alter table Users change `DisplayName` display_name` , @columnType));
prepare stmt @sqlstmt;
execute stmt;
deallocate prepare stmt
MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'no' in 'field list'
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
   at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
   at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
   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, XDocument targetModel, IEnumerable`1 operations, 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.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()
Unknown column 'no' in 'field list'

我认为错误会在这里 - &gt;

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 = `Users` and column_name = `DisplayName` );

no 是错误。

请帮我纠正此错误。

0 个答案:

没有答案