Search code examples
ef-core-6.0

Database.Migrate() .net6 not launch migrations


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.


Solution

  • Solution:

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

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