samedi 27 juin 2015

Entity Framework migration AddColumn(), followed by Sql(@"Update of that column

Given this migration:

public override void Up()
{
    AddColumn("dbo.ClientData", "ExFirstName", c => c.String(maxLength: 2));
    Sql(@"UPDATE [ClientData]
            SET ExFirstName = LEFT(EX,2)
          WHERE RandomEX = 0
            AND LEN(EX) = 8");

Then I generate migrations as sql script to give to dba

ALTER TABLE [dbo].[ClientData] ADD [ExFirstName] [nvarchar](2)
UPDATE [ClientData]
                SET ExFirstName = LEFT(EX,2)
              WHERE RandomEX = 0
                AND LEN(EX) = 8

Then when run it fails on the UPDATE because the ALTER TABLE isn't followed by a "GO".

Here is the exact error: Msg 207, Level 16, State 1, Server MYSERVER\SQLSVR, Line 34 Invalid column name 'ExFirstName'.

I confirmed that by running above sql in ssms w/ and w/o the GO.

Baring splitting this and some of my other ~100 EF migrations up into two EF migrations do I have other options either in the EF configuration or within SQL Server?

Aucun commentaire:

Enregistrer un commentaire