Skip to content

Commit

Permalink
feat: add migrate async
Browse files Browse the repository at this point in the history
  • Loading branch information
danielNg91 committed Oct 24, 2023
1 parent 8447b19 commit 8dcd01c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Api/Configurations/AppConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,13 @@ public static WebApplication UseLoggingInterceptor(this WebApplication app)
});
return app;
}

public static async Task ApplyMigrations(this IServiceProvider serviceProvider)
{
using var scope = serviceProvider.CreateScope();
await using ApplicationDbContext dbContext =
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await dbContext.Database.MigrateAsync();
}
}
}
3 changes: 2 additions & 1 deletion Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();

var configurationManager = builder.Configuration;
#region Add configurations to Services
{
var configurationManager = builder.Configuration;
var configuration = configurationManager.GetSection(nameof(AppSettings));
services.Configure<AppSettings>(configuration);
builder.UseSerilog(configuration);
Expand All @@ -28,6 +28,7 @@
{
app.UseSwagger();
app.UseSwaggerUI();
await app.Services.ApplyMigrations();
}

app.UseLoggingInterceptor();
Expand Down

0 comments on commit 8dcd01c

Please sign in to comment.