ASP.NET: Check if starting from migration is running

I have code in mine ConfigureServicesthat does not work when doing the migration:

dotnet ef migrations list

I am trying to add a certificate, but it cannot find the file (it works when the project as a whole starts). So, there is a way to do something like this:

if (!CurrentEnvironment.IsMigration()) {
    doMyStuffThatFailsInMigration()
}

That way, I could save my code as it is, but just execute it when it doesn't start during the migration process.

thank

+4
source share
1 answer

My current migration detection solution did not happen:

using System.Linq;
// app is of type IApplicationBuilder
// RegisteredDBContext is the DBContext I have dependency injected
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
        {
            var context = serviceScope.ServiceProvider.GetService<RegisteredDBContext>();
            if (context.Database.GetPendingMigrations().Any())
            {
                var msg = "There are pending migrations application will not start. Make sure migrations are ran.";
                throw new InvalidProgramException(msg);
                // Instead of error throwing, other code could happen
            }
        }

, . EnsureDatabase, , .

context.Database . GetMigrations GetAppliedMigrations.

0

Source: https://habr.com/ru/post/1677441/


All Articles