-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from intive/feature/PATRO23-102-Run-migrations…
…-on-startup feature/PATRO23-102-Add-migrations-run-on-startup
- Loading branch information
Showing
4 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
using Microsoft.EntityFrameworkCore; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Intive.Patronage2023.Shared.Abstractions; | ||
|
||
/// <summary> | ||
/// Class contains methods that extend ApplicationBuilder. | ||
/// </summary> | ||
public static class ApplicationBuilderExtension | ||
{ | ||
/// <summary> | ||
/// Enables migrations on startup for provided dbcontext class. | ||
/// </summary> | ||
/// <typeparam name="T">DbContext class to make migrations for.</typeparam> | ||
/// <param name="app">IApplicationBuilder.</param> | ||
/// <returns>Updated IApplicationBuilder.</returns> | ||
public static IApplicationBuilder InitDatabase<T>(this IApplicationBuilder app) | ||
where T : DbContext | ||
{ | ||
using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>()!.CreateScope()) | ||
{ | ||
var dbContext = scope.ServiceProvider | ||
.GetRequiredService<T>(); | ||
dbContext.Database.Migrate(); | ||
} | ||
|
||
return app; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters