MySql with EF5 CodeFirst Migration: Error when changing table column name (Fluent API)

Topics: General
Jan 5, 2013 at 11:03 AM

I am using EF5 CodeFirst with MySql DB.

I have a class 'User'

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

And I want to change the table column DisplayName to display_name.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
                .Property(u => u.DisplayName)
                .HasColumnName("display_name");
        }
Then I executed the
Add-Migration ChangeDisplayName
and
Update-Database -Verbose

and got an error.

    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'

I think that the error would be in here ->

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' is the error.


Please help me to correct this error. Thank You.