Base solution for your next web application
Open Closed

Entity framework code first - DBContext.migrate() in startup #8888


User avatar
0
sneddo created

Hello,

I want my app to auo migrate any db changes on start up. This is normally pretty simple in that you just call Migrate() on the DB context.

Something like:

using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope()) { var context = serviceScope.ServiceProvider.GetService<T>(); context.Database.Migrate(); }

Where T is DbContext. In this case MyAppDbContext. But the service provider is unable to resolve the db context so I assume there is another way to do that?


3 Answer(s)
  • User Avatar
    0
    musa.demir created

    Is multitenancy enabled on your app?

  • User Avatar
    0
    sneddo created

    Yes it is.

  • User Avatar
    0
    musa.demir created

    To migrate db you can check *.Migrator project. You should use its same methods because your tenants can have separate database.