Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DbMigrator - Problems with existing migrations #62

Closed
forsti83 opened this issue Oct 28, 2020 · 6 comments
Closed

DbMigrator - Problems with existing migrations #62

forsti83 opened this issue Oct 28, 2020 · 6 comments
Assignees

Comments

@forsti83
Copy link

Hi!

We have migrated our application from EF 6.4.4 to EF Classic 7.1.34. We are using MS SQL Server as database. Usually the application user executes the pending migrations at application start. Internally the DbMigrator class is used for this in code:

var migrator = new DbMigrator(new Context.Configuration());
migrator.Update();

Since the migration to EF Classic we are facing problems with migrations of this type:

public override void Up()
{
	AddColumn("dbo.TableName", "ColumnName", c => c.Int(nullable: false));

	Sql("UPDATE dbo.TableName SET ColumnName = 123");
}

We are getting the exception that the column "ColumnName" is invalid at runtime, when the SQL-statement is executed. We can fix this problem with wrapping the SQL statement in an EXEC:

public override void Up()
{
	AddColumn("dbo.TableName", "ColumnName", c => c.Int(nullable: false));

	Sql("EXEC('UPDATE dbo.TableName SET ColumnName = 123')");
}

It seems that the DbMigrator is not finishing the batch after the AddColumn statement like EF 6.4.4 did. Is there any explanation for this or even a config option for getting the old behaviour?

@JonathanMagnan JonathanMagnan self-assigned this Oct 28, 2020
@JonathanMagnan
Copy link
Member

Hello @forsti83 ,

I believe disabling the option BatchMigration in your context constructor will make the job:

public BloggingContext() : base((My.ConnectionString))
{
	this.Configuration.BatchMigration.IsEnabled = false;
}

Let me know if that fixed it.

Best Regards,

Jon

@forsti83
Copy link
Author

Hello @JonathanMagnan ,

that fixed it. Thank you very much! Has EF Classic another default value for this config setting than EF?

Best Regards,
Andreas

@JonathanMagnan
Copy link
Member

Hello Andreas,

I'm not sure to understand you question.

If you ask if there is another config that could affect the migration, I believe that's the only one

@forsti83
Copy link
Author

forsti83 commented Oct 29, 2020

Hello Jon,

no, the question is, if you have a clue, why our migrations are working with EF 6.4.4 (without setting the BatchMigration config option to false), but are failing with EF Classic 7.1.34 (again without setting the BatchMigration config option to false)?

Thank you and best regards,
Andreas

@JonathanMagnan
Copy link
Member

Oh,

It fail in EF Classic since by default, we batch command.

Which means, instead of executing for example 100 SQL command to create all your tables and index, perhaps only 1-2 commands are executed.

So when batching the migration, this part will be executed way faster.

However, a custom script like your make SQL fail unless it's in a execute method.

This is similar to creating first a table:

CREATE TABLE A
(
	ColumnInt INT
)

then executing the following SQL

ALTER TABLE dbo.A
ADD ColumnInt2 INT

UPDATE A
SET ColumnInt2 = 1

even if somewhat valid, you will get the same error.

However, you found a workaround to make it works with the EXEC.

When directly in SSMS, you can also use the "GO" workaround:

ALTER TABLE dbo.A
ADD ColumnInt2 INT

GO

UPDATE A
SET ColumnInt2 = 1

GO

Let me know if that explains correctly the current behavior.

@forsti83
Copy link
Author

Hi Jon,

thank you very much for the detailed information!

Best regards,
Andreas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants