214176 (6) [Avatar] Offline
#1
I have an ASP.NET Core project that will be hosted on Azure App Services for production that calls a custom database Initialize function from within the Program.Main(string[] args) static function to load the applications default data and create the default admin user. The EF Core code is in a separate class library and EF Core migrations are used during development within Visual Studio 2017 on local machine. To maintain using migrations in development and publishing updates to Azure production that will apply those migrations, which DbContext.Database call should be made in the top of the Initialize function the Migrate() or the EnsureCreated() function. I want to make sure the database exists but also want to make sure that any migrations are applied. Thanks...

Orgbrat
Jon P Smith (39) [Avatar] Offline
#2
Hi Orgbrat,

Have a look at section 5.9.1 in the book, where I explain how to apply a migration on startup - listing 5.10 has some code. Calling the migration command will create a database if one isn't there, or it will apply any migrations needed. If no migrations are needed it doesn't do anything.

However I will be updating this section in the next release because Microsoft's recommendation is to apply a migration in production using SQL change scripts produced from the migration - see https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/migrations#appling-migrations-in-production.

I hope that helps
214176 (6) [Avatar] Offline
#3
Thanks for the quick reply as it was much appreciated. Can you tell me when the next update will be so I can see the changes you are recommending.

Orgbrat
Jon P Smith (39) [Avatar] Offline
#4
Hi Orgbrat,

I cover how to apply a SQL change script is described in section 11.4.

You can output a SQL script from a migration using the Script-Migration command. See Microsoft's docs on this at https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell.

The book will be updated to detail this in the final update, which I am doing now. It will be available in January 2018.