Base solution for your next web application
Open Closed

running EntityFramework Migrations on Startup for a Multi-Tenant Application #10129


User avatar
0
sedulen created

Prerequisites

  • What is your product version? 6.9.0
  • What is your product type (Angular or MVC)? Angular
  • What is product framework type (.net framework or .net core)? .net core

Looking at [Question #6960](https://support.aspnetzero.com/QA/Questions/6960/Apply-migrations-update-database-on-application-start) The provided answer is awesome, but it looks like it only runs migrations for the HOST database, and I'm trying to adapt this for a MultiTenant setup.

So I have 2 questions:

  1. Referencing the code in the MultiTenantMigrateExecuter.cs class in the Migrator project, do you foresee any issues retrieving the list of Tenants that have separate connectionStrings, and running CreateOrMigrateForTenant ?

  2. Would running migrations at this PostInitialize method of the XXXEntityFrameworkCoreModule possibly have any interference with IBackgroundJobManager ? Would it be advisable to resolve the IBackgroundJobManager and call .Stop and .WaitToStop before running the Migrator functions? (and then calling .Start again when migrations are complete) ?

Thanks!


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @sedulen

    1. When the app starts, the database will not be migrated with the latest migrations, so you might get an exception before executing the migrations.
    2. I think, if you don't get an error on step 1, this will be fine.

    But, if there is no special reason for implementing this, we suggest running Migrator to update databases.

  • User Avatar
    0
    sedulen created

    thanks @ismcagdas

    so the concern is that parts of the application may start before migrations are completed, and that may cause an exception?

    understood on the suggestion of running the Migrator to update the databases.

    what I'm working on is a way to hot-swap Docker Containers, either through AKS or Azure AppServices, where I can deploy an upgrade to the application to a deployment slot, or to a second set of Docker Containers, warm them up, and then hot swap the traffic over to the new instances.

    so having the ability for the application to self-migrate would be a huge help.

    in looking at the documentation provided for wiring up ANZ into Azure DevOps Pipelines, we don't have connectivity to the SQL database. That connectionString is private to our hosting environment. So for using the Migrator or for generating the EF Migration SQL Scripts, we don't have access to the SQL DB in our DevOps pipeline.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @sedulen

    Thank you for the explanation. I understood the point. AspNet Zero's Azure DevOps documentation uses script generation but I suggest deploying Migrator app to Azure so it can access the database. Does that work for you ?

    If that doesn't work, we can work on running migrations on app startup. At least, we can try to create a tutorial or document for it. Please let us kınow if deploying Migrator to Azure works for you or not.