fengling-platform/Fengling.Platform.Infrastructure/DesignTimeApplicationDbContextFactory.cs
2026-02-21 15:13:44 +08:00

28 lines
1.1 KiB
C#

namespace Fengling.Platform.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.DependencyInjection;
public class DesignTimeApplicationDbContextFactory : IDesignTimeDbContextFactory<PlatformDbContext>
{
public PlatformDbContext CreateDbContext(string[] args)
{
IServiceCollection services = new ServiceCollection();
services.AddMediatR(c =>
c.RegisterServicesFromAssemblies(typeof(DesignTimeApplicationDbContextFactory).Assembly));
services.AddDbContext<PlatformDbContext>(options =>
{
options.UseNpgsql("Host=localhost;Database=fengling_platform;Username=postgres;Password=postgres",
b =>
{
b.MigrationsAssembly(typeof(DesignTimeApplicationDbContextFactory).Assembly.FullName);
});
options.UseOpenIddict();
});
var provider = services.BuildServiceProvider();
var dbContext = provider.CreateScope().ServiceProvider.GetRequiredService<PlatformDbContext>();
return dbContext;
}
}