So I have an application with a ton of migrations made by Entity framework.
We want to get a script for all the migrations at once and using the -Script
tag does work fine.
However...it does not add GO
statements in the SQL giving us problems like Alter view should be the first statement in a batch file...
I have been searching around and manually adding Sql("GO");
help with this problem but only for the entire script. When I use the package console manager again it returns an exception.
System.Data.SqlClient.SqlException (0x80131904): Could not find stored procedure 'GO'.
Is there a way to add these GO
tags only when using the -Script
tag?
If not, what is a good approach for this?
Note: we have also tried having multiple files but since we have so many migrations, this is near impossible to maintain every time.
Turn out the concept exist deep in the
SqlServerMigrationSqlGenerator
as an optional argument forStatement(sql, batchTerminator)
. Here is something based on Skyp idea. It works both in -script mode or not. The GOs are for different operations than for Skyp only because our needs are a little different. You then need to register this class in theConfiguration
as per Skyp instructions.If you are trying to alter your view using
Sql('Alter View dbo.Foos As etc')
, then you can avoid theshould be the first statement in a batch file
error without addingGO
statements by putting the sql inside anEXEC
command:Sql(EXEC('Alter View dbo.Foos As etc'))
In order to change the SQL Generated by entity framework migrations you can create a new
SqlServerMigrationSqlGenerator
We have done this to add a GO statement before and after the migration history:
then add in the
Configuration
constructor (in theMigrations
folder of the project where you DbContext is) so that it uses this new sql generator:So now when you generate a script using the -Script tag, you can see that the
insert into [__MigrationHistory]
is surrounded byGO
Alternatively in your implementation of
SqlServerMigrationSqlGenerator
you can override any part of the script generation, theInsertHistoryOperation
was suitable for us.Just replace the current statement with a .Replace("GO", "");