Using System.Data.Entity.Migrations.DbMigrator
I have a code-first migration, where I purposefully added an error at the end of the Up() method.
Sql("SELECT * FROM DOES_NOT_EXIST"); // Simulate error in migration
Now calling Migrate()
throws SqlException
as expected, but the interesting part is when I investigate with Sql Profiler.
In the profiler I can see queries, which are part of the migration (dropping or adding columns, modifying data, etc.)
The last query is in fact SELECT * FROM DOES_NOT_EXIST
.
Why all these previous queries from a migration seem to have no effect?
Almost like some kind of transaction was rolled back. But the migration isn't running within a transaction...
Full Up()
code:
public override void Up()
{
AddColumn("dbo.DummyEntities", "DifferentValue", c => c.String());
DropColumn("dbo.DummyEntities", "Value");
Sql(@"INSERT INTO [dbo].[DummyEntities]([DifferentValue]) VALUES ('MigratingToInvalid1')");
Sql(@"INSERT INTO [dbo].[DummyEntities]([DifferentValue]) VALUES ('MigratingToInvalid2')");
// Simulate invalid migration command, this will throw an exception
Sql("SELECT * FROM DOES_NOT_EXIST");
}