Multi tenant MVC4 application and automatic DB migrations with Entity Framework? RRS feed

  • Question

  • Hello.

    I've built a multi tenant MVC4 application that use a specific database depending on the hostname.

    The site binds to all of our customers hostnames.
    - If a visitor surfs to 'domain1.com', the 'domain1.com' database is used.
    - If a visitor surfs to 'domain2.com, the 'domain2.com' database is used.

    Automatic migrations are normally placed in Application_Start():

    Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyProject.Models.MyProjectContext, MyProject.Migrations.Configuration>());

    This will run the migrations on application start. However, since my application responds to several hostnames, only one database will be migrated. When i switch to another hostname, that database IS NOT migrated since the application is already loaded into memory on the server.

    I solved this by moving the above line of code to Session_Start().

    Is this good practice? Is there a better solution?

    Thanks in advance,

    Friday, April 26, 2013 9:03 AM