Rename column with Entity framework

Asked

Viewed 118 times

2

I would like to know how I can solve the following problem. I have the class below:

namespace CustomizandoCodeFirstMigrations.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class AlteraTelefoneFixoRenomeiaCelular : DbMigration
    {
        public override void Up()
        {
            RenameColumn("dbo.Pessoas", "Celular", "TelefoneCelular");
            AlterColumn("dbo.Pessoas", "TelefoneFixo", c => c.String(nullable: false, unicode: false));
        }

        public override void Down()
        {
            RenameColumn("dbo.Pessoas", "TelefoneCelular", "Celular");
            AlterColumn("dbo.Pessoas", "TelefoneFixo", c => c.String());
        }
    }
}

And when I type in the package manager console the update-database command I get as a response:

Subquery Returns more than 1 Row

  • I need two more pieces of information before I answer: the return of select * from Pessoas and select * from __MigrationHistory.

  • @Gypsy omorrisonmendez The return of select * from pessoas is: 1, João Batista, [email protected], (12)1234-5678, (12)99778-0000 and the return of select * from __MigrationHistory is 201512231623056_InitialCreate, CustomizandoCodeFirstMigrations.K19Context, blob, 6.1.3-40302

  • 1

    If you consider it important, when running the update-database -force -verbose command, I receive as part of the reply: 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 = 'Pessoas' and column_name = 'Celular');
set @sqlstmt := (select concat('alter table PeoplechangeCellular Telephone ' , @columnType));
prepare stmt from @sqlstmt;
execute stmt;
deallocate prepare stmt;

  • @I appreciate the help. A colleague from a Facebook group explained to me that it could be the fact that there is another table with the same name in another schema. I deleted the previous schema and ran the update-database command and everything worked.

  • I’ll just synthesize an answer, okay?

1 answer

0


At your command verbose, we have:

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 = 'Pessoas' 
                     and column_name = 'Celular'); 
 set @sqlstmt := (select concat('alter table Pessoas 
                                 change Celular TelefoneCelular ', @columnType)); 
 prepare stmt from @sqlstmt; 
 execute stmt; 
 deallocate prepare stmt;`

The command @columnType received two occurrences of Pessoa, so your mistake.

Erase the schema is a path, but I am in favour of a softer solution, which would delete only the table Pessoa of this other schema.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.