payetools-steve
payetools-steve

Reputation: 4030

Is there a way in EF Core to generate SQL scripts when using separate migration assemblies

Following Microsoft's instructions on Using a Separate Migrations Project, I have build a solution model that looks something like the following:

\
 +-- Application.csproj
 +-- ProjectContainingDbContext.csproj
 +-- MigrationProject.SqlServer.csproj
 +-- MigrationProject.Postgres.csproj

I'm using the following syntax to generate the content of the migration assemblies:

dotnet ef migrations add Initial --project MigrationProject.SqlServer.csproj

This step works fine. I'm not actually using the migration assembly but rather I need to generate the SQL as I'm using DbUp to apply migrations in production. However, it doesn't appear script generation respects the --project flag, i.e., the following appears to look in the same assembly as the DbContext and therefore fails to generate the migration SQL:

dotnet ef migrations script --output path_for_sql_files --project MigrationProject.SqlServer.csproj

Can anyone suggest a workaround for this issue?

I'm using EF Core 9.0.2.

Related, the command dotnet ef migrations -has-pending-model-changes also failed to respect the --project argument (raised as feature request on GitHub as EF Core Issue 35637.)

Upvotes: 0

Views: 28

Answers (1)

payetools-steve
payetools-steve

Reputation: 4030

So it turns out that this all works fine, if you build the migration assemblies prior to running either the dotnet ef migrations script command or the dotnet ef has-pending-model-changes command.

Probably obvious in retrospect, but wasn't to me and the documentation doesn't say anything about needing to build assemblies - remember that when you run the dotnet ef migrations add command, it does a build of the assembly containing the DbContext before and after...

So, what you need in build pipeline is:

dotnet build MigrationProject.SqlServer.csproj
dotnet ef migrations script --output path_for_sql_files --project MigrationProject.SqlServer.csproj

Upvotes: 0

Related Questions