Reputation: 3788
I have code-first generated database. In it I have a bool column in a table. This column needs to be changed to an enum. Problem is the table already has data, which has to be somehow converted. What I have bool IsA: true/false what I need enum MyEnum: A,B,C,D I need the values of IsA converted so that true maps to A and false maps to B. How can I do this?
Upvotes: 0
Views: 1100
Reputation: 13488
You can try to do this at migration class:
public partial class fromBool2Enum : DbMigration
{
public override void Up()
{
//add new column:
AddColumn("dbo.MyTable", "IsA_TEMP", c => c.Int());
//transfer data to just created column
Sql("Update dbo.MyTable set IsA_TEMP = case when IsA then 0 else 1 end");
//0(i.e. A) and 1(i.e. B) is just example, correct it on your own, depending on enum's declaration
//Drop old column
DropColumn("dbo.MyTable", "IsA")
//Rename new column to initial name
Sql("EXEC sp_rename 'dbo.MyTable.IsA_TEMP', 'IsA', 'COLUMN'");
}
public override void Down()
{
//corresponding reverse code...
}
}
Upvotes: 2