Skip to content

Instantly share code, notes, and snippets.

@esersahin
Created January 30, 2024 13:02
Show Gist options
  • Save esersahin/4841891526b99c8a8e6b1ac7626ebdcf to your computer and use it in GitHub Desktop.
Save esersahin/4841891526b99c8a8e6b1ac7626ebdcf to your computer and use it in GitHub Desktop.
Issue-7
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using TbdFriends.WaterDrinkWater.Data.Contexts;
namespace TbdFriends.WaterDrinkWater.Data.Extensions;
public static class MigrationExtension
{
public static void ApplyMigrations(this IServiceProvider services)
{
using var scope = services.CreateScope();
var factory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<ApplicationDbContext>>();
using var context = factory.CreateDbContext();
context.Database.Migrate();
}
}
@esersahin
Copy link
Author

To use the extension, add the following line to program.cs.

app.Services.ApplyMigrations();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();

app.Services.ApplyMigrations();

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment