0

I am trying to run the migrations automatically with net6

In program.cs

     using (var scope = serviceProvider.CreateScope())
        {
            var db = scope.ServiceProvider.GetRequiredService<ApiDbContext>();

            var pendingMigrations = db.Database.GetPendingMigrations().ToList();
            var migrations = db.Database.GetMigrations();

            var items = db.Items.ToList();

            db.Database.Migrate();
        }

Values:

pendingMigrations = 0
migrations = 0 
items = nº elements in db, is correct

ApoDbcontext can access to db, but does not find unlaunched migrations

I can launch migrations in powershell, and run ok

Project (DDD layers):

|- Solution 
  |- Api 
     |- Program.cs
  |- Aplication
  |- Domain
  |- Infrastructure 
     |- Migrations (folder)
     |- ApiDbContext.cs

Thanks.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Meldrel
  • 155
  • 1
  • 2
  • 9

1 Answers1

0

Solution:

dotnet ef --startup-project ../Project.Api/ migrations add InitialModel

https://stackoverflow.com/a/54201822/4907049

Meldrel
  • 155
  • 1
  • 2
  • 9
  • Doesn't answer the question of why migrations aren't being applied with code. Here you are doing it manually in the terminal. – lchras Jul 06 '23 at 12:58