Migrations Bundles
We currently have dotnet ef migrations script and database update to apply migrations. Unfortunately, they each have their shortcomings.
migrations script is pretty good at producing SQL scripts (there are some known issues with the scripts), but figuring out what to do with those scripts may not be obvious. Publish from Visual Studio wraps these scripts up in a web deployment package and uses MSDeploy to run them on the server. This works well for SQL Server on Windows, but nothing else.
database update is a lot better at applying migrations. It doesn't have any of the issues the SQL script does. Unfortunately, it requires the .NET Core SDK, your app's source code, and a direct connection to the production database. Having all of these things available where you need them is't always possible.
I propose we introduce a new migrations bundle command. This would create a self-contained EXE that you could use to apply migrations. It would behave the same as database update and let you specify the connection string. This would make SSH/Docker/PowerShell deployment a lot easier. You wouldn't need to copy your source files to a server inside the firewall and you wouldn't need to install the .NET Core SDK. Here is an example of what it might look like to use SSH:
dotnet ef migrations bundle
scp bundle [email protected]:./
ssh [email protected] "./bundle --connectionString ${DEPLOYMENT_CONNECTION_STRING}"
ssh [email protected] "rm bundle"
Taking this further, Visual Studio could do all of this for you like they do today on MSDeploy but when you're deploying to Docker. They could even create a new SSH deployment experience.
TODO
- [x] Add
--outputto allow renaming the bundle - [x] Match TargetFramework, ~~RuntimeIdentifier, and SelfContained~~ of startup project by default
- ~~Add
--no-self-contained~~
- ~~Add
- [x]
--self-containeddoesn't workerror NETSDK1031: It is not supported to build or publish a self-contained application without specifying a RuntimeIdentifier. You must either specify a RuntimeIdentifier or set SelfContained to false.
- [x]
--runtimedoesn't work between Windows and other OSes. We look for *.exe based on the current runtime instead of the target runtime. - [x] Add automated tests
- [x] Add
--forceto overwrite an existing bundle #25271 - [x] Refine command output #25274
- [x]
--configurationand--runtimeoptoins are repeated in help
@bricelam Why not use dacpac? I have not seen any better approach for MSSQL.
We try to embrace sqlproj (in this sense it's DB first) + reverse engineer code first on every change + dacpac migrations.
dacpac is perfect for DB-first; keep using it there. However, it's fundamentally different from Migrations which are designed for a code-first workflow. Also, dacpac is SQL Server-only and a big part of these bundles is to improve the experience when using other databases like PostgreSQL, MySQL, and Oracle.
Fun fact, the first prototype of Migrations was based on dacpac.
FWIW, "EF Core Power Tools" supports Reverse Engineering directly from a dacpac / SQL Database project (.sqlproj)
migrations script is pretty good at producing SQL scripts (there are some known issues with the scripts)
Could you elaborate on the issues?
database update is a lot better at applying migrations.
In what way(s) is update better?
I take it the SQL isn't the same if I was to look at trace logs of the two different update methods?
The SQL is the same, the differences (and issues) stem from things like when transactions begin and end, the if blocks (some statements aren’t allowed inside), and how the script is parsed (some objects are created at the beginning of the script but the parser/compiler errors because later statements reference them.
Items to address issues with the script: #7681 #12911 #14746 #19740
Team note: make sure to consider how bundles interact with continuous deployment systems. See #9841.
Unfortunately, while we have made good progress in understanding the space here, we are deferring release of the migrations bundles feature until after the EF Core 5.0 release to provide more time for feedback. The current plan is to get something into preview near the beginning of the EF Core 6.0 milestone so that we have enough time for feedback, integration, and testing before committing.
I'm not sure if it is the right place, but here are my considerations: It would be interesting if the migrations bundle could be built and run on both Windows/Linux platforms.
Currently I'm developing using a Windows machine and deploying to a Linux server. Also, my CI workflow uses Linux machines under the covers. So being able to choose the target platform would be great.
This is currently an issue I'm interested in we currently work with multiple databases including postgres, mysql, sql server. Using azure pipelines we have not found a suitable way to deploy migrations since migration script will not work with postgres.
@JohnnySixStrings migration scripts generally work fine with PostgreSQL - I'm guessing you're running into issues with maintaining migrations with multiple providers (see this doc page).
If you're encountering specific issues with PostgreSQL migrations, that probably isn't related to this issue - can you please open an issue on https://github.com/npgsql/efcore.pg?
A common use case for me is that the production database is not accessible from my workstation - not directly, and not over a tunnel either - typically due to clients' on-prem requirements.
So the proposed bundle -> copy -> run -> cleanup workflow would work well in these cases.
One thing to consider is that the putative bundle executable needs to be runnable on a different platform to the one where it was built. For example, my use case might require building the bundle on an Ubuntu box then copying it and running it against an MS SQL database on a Windows Server box.
I realise that we can target runtimes when building our applications, so I wonder whether a) this is necessary and b) whether this is possible for bundle executables.
With migrations bundle ideally It would be possible to also run the migrations using the dotnet runtime image instead of the dotnet sdk image which is bigger and not ideal for final stages of docker containers.
I see no problem using the dotnet sdk image for the build stage of the bundle but actually running the migrations bundle should be using the more lightweight runtime image instead.
Also migration bundles ideally shoud run on both docker for linux OR on Windows containers, as the client's existing infrastructure might require that.
📢 Status update
I got an end-to-end working. There are still a lot of details to sort out before sending a PR...

An initial implementation was just merged. It'll be in Preivew 7 (August).
You can try it out using the daily builds:
dotnet tool update -g dotnet-ef --version 6.0.0-* --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json
dotnet ef migrations bundle
./bundle
Or from Visual Studio:
Bundle-Migration
.\bundle.exe
See TODO list in issue description for what's still missing.
Hey @bricelam ...should this work with SQLite yet? I'm targeting 6.0.0-rc.1.21409.2 for packages and the tools. Getting
Method 'AppendIdentityWhereCondition' in type 'Microsoft.EntityFrameworkCore.Sqlite.Update.Internal.SqliteUpdateSqlGenerator' from assembly 'Microsoft.EntityFrameworkCore.Sqlite, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.
If that's new to you then maybe it's related to my model and I can dig further. But here is what's in the UP of my one and only migration file
migrationBuilder.CreateTable(
name: "People",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
FirstName = table.Column<string>(type: "TEXT", nullable: true),
LastName = table.Column<string>(type: "TEXT", nullable: true),
Name_FirstName = table.Column<string>(type: "TEXT", nullable: true),
Name_LastName = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_People", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Address",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
Street = table.Column<string>(type: "TEXT", nullable: true),
PostalCode = table.Column<string>(type: "TEXT", nullable: true),
AddressType = table.Column<int>(type: "INTEGER", nullable: false),
PersonId = table.Column<Guid>(type: "TEXT", nullable: false),
StreetLine2 = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Address", x => x.Id);
table.ForeignKey(
name: "FK_Address_People_PersonId",
column: x => x.PersonId,
principalTable: "People",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Address_PersonId",
table: "Address",
column: "PersonId");
Also note: database-update works from this migration.
Note: I removed the owned entity from the model, trashed original migrations and created a new migration from scratch. Same error when trying to execute the new bundle so it wasn't the owned entity. :)
@julielerman this is most likely as a result of mixing EF Core versions - can you check in your csproj that all versions are exactly the same etc.?
@roji Hi there. Only package refs are 6.0.0-*
I am targetting daily builds with nuget.config
<packageSources>
<add key="dotnet6" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
In projects.assets.json all efcore packages are the same version:
"Microsoft.EntityFrameworkCore.Sqlite/6.0.0-rc.1.21409.2": {
Microsoft.EntityFrameworkCore.Relational/6.0.0-rc.1.21409.2": { "Microsoft.EntityFrameworkCore.Design/6.0.0-rc.1.21409.2": {
And I installed the same version of the efcore tools as per Brice's note above.
I'm going to test against sql server now (just had to get my container running :) )
same with sql server. Ok...so it's me. I just don't know what... :) Shall I drop my little sample (or just project.assets.json) on github? Wait for preview 7?