I am setting up a WebApi in .NET Core 2.0. I will be using Entity Framework Core as ORM. Whole app will be deployed as Docker Container. The thing that disturbs me a bit is the way of handling DB migrations in this case. I mean PRODUCTION environment. Here is what I managed to research:
dotnet ef database update
Any other suggestions ? Or what is the best, most proper solution ?
Regards
Migrations are enabled by default in EF Core. They are managed by executing commands. If you have Visual Studio, you can use the Package Manager Console (PMC) to manage migrations. Alternatively, you can use a command line tool to execute Entity Framework CLI commands to create a migration.
Right click your web project, click publish, use web deploy, go to your databases, target your new database, ensure Execute Code First Migrations is checked (this will run all the migrations you've done for your localdb on your new database).
EF core doesn't support automatic migrations . So you have to do it manually. From the perspective of automatic migrations as a feature, we are not planning to implement it in EF Core as experience has showed code-base migrations to be a more manageable approach.
In my opinion, it is your first point (Database.Migrate() due startup) that meets mostly our use case. So for me, it`s currently the preferred way to do that.
We have some additional constellations in the starting up process:
Docker container locally only (for dev environment and testing for sure)
Own startup project which executes the Database.Migrate() part (cause we have more than one project with its own database)
Additional project with the actually API website :)
Production environment with Azure SQL server (Published and deployed through Azure DevOps pipeline
Migrations are created in its own project via dotnet ef ...
dotnet ef migrations add "your migration name" --startup-project "path to your actually API" --context "database context name"
Important: you have to change the working directory to the migration project first in order to use another startup project but generate the migration files in the "migration project"
In our case, it works fine with different APIs with their own databases behind the scene.
Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With