-
Notifications
You must be signed in to change notification settings - Fork 2
/
HostExtensions.cs
32 lines (26 loc) · 1.15 KB
/
HostExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using Microsoft.EntityFrameworkCore;
namespace Template;
/// <summary>
/// Provides extension methods for <see cref="IHost"/>.
/// </summary>
public static class HostExtensions
{
/// <summary>
/// Asynchronously applies any pending migrations for the specified database context.
/// </summary>
/// <typeparam name="TDbContext">The type of the database context to apply migrations for.</typeparam>
/// <param name="host">The host that provides access to the app's services.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public static async Task MigrateAsync<TDbContext>(this IHost host)
where TDbContext : DbContext
{
await using var scope = host.Services.CreateAsyncScope();
var db = scope.ServiceProvider.GetRequiredService<TDbContext>();
var migrations = await db.Database.GetPendingMigrationsAsync();
if (!migrations.Any())
return;
db.Database.Migrate();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Applied {count} pending database migration(s).", migrations.Count());
}
}