I am trying to change a CLOB column to VARCHAR(2000) in oracle using EF 6 migration. When EF scaffold the migration file, I have the following line of code in my Up() method:
AlterColumn("MTA.PLAN_SHEETS", "PLANSHEET_NAME", c => c.String(maxLength: 2000, unicode: false));
When I run the migration I get this error
invalid modification of columns
I know that in Oracle we cannot change a Clob to Varchar directly:
How to change a dataype CLOB TO VARCHAR2(sql)
What is the best approach to change a Clob to Varchar in Entity Framework migration?
There is the same problem in probably all RDBMSs. I always use a code similar to this and I think (I'm quite sure) that this is the only way to do it.
AddColumn("MTA.PLAN_SHEETS", "TMP_COLUMN", c => c.String(maxLength: 2000, unicode: false));
Sql(@"UPDATE MTA.PLAN_SHEETS SET TMP_COLUMN = PLANSHEET_NAME");
DropColumn("MTA.PLAN_SHEETS", "PLANSHEET_NAME");
RenameColumn("MTA.PLAN_SHEETS", "TMP_COLUMN", "PLANSHEET_NAME");