nightingale2k1
nightingale2k1

Reputation: 10335

Best practice for deploying migrations on Production Server? (DOTNET Core / 5)

I use .NET 5 (Core) for my app. I would like to know what is the best practice to allow server doing migrations script on Production ? Should I create migrations script --idempotent and run dotnet ef database update on the server ? Or should I give the client pieces of migration scripts and do the update on there ? Should I put automating the migrationscript on the compiled app ?

Upvotes: 1

Views: 956

Answers (1)

wbail
wbail

Reputation: 566

Here's the Microsoft documentation about migrations in production.

https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/applying?tabs=dotnet-core-cli#apply-migrations-at-runtime

You can call the Database.Migrate(); inside the constructor of your Db Context class or in the dependency inversion process create a scope of the Db Context class and call:

var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
db.Database.Migrate();

But, be aware of the warning:

  • Carefully consider before using this approach in production. Experience has shown that the simplicity of this deployment strategy is outweighed by the issues it creates.
  • Consider generating SQL scripts from migrations instead. Don't call EnsureCreated() before Migrate(). EnsureCreated() bypasses Migrations to create the schema, which causes Migrate() to fail.

Upvotes: 3

Related Questions