最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.)

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.)

Share Improve this question edited Feb 16 at 6:40 payetools-steve asked Feb 16 at 6:35 payetools-stevepayetools-steve 4,0303 gold badges29 silver badges24 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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
发布评论

评论列表(0)

  1. 暂无评论