persistence/Persistence.Database.Postgres/DesignTimeDbContextFactory.cs

29 lines
1.1 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Npgsql;
using Persistence.Database.Model;
namespace Persistence.Database.Postgres;
/// <summary>
/// Фабрика контекста для dotnet ef миграций
/// </summary>
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<PersistencePostgresContext>
{
public PersistencePostgresContext CreateDbContext(string[] args)
{
var connectionStringBuilder = new NpgsqlConnectionStringBuilder();
connectionStringBuilder.Host = "localhost";
connectionStringBuilder.Database = "persistence";
connectionStringBuilder.Username = "postgres";
connectionStringBuilder.Password = "q";
connectionStringBuilder.PersistSecurityInfo = true;
var connectionString = connectionStringBuilder.ToString();
var optionsBuilder = new DbContextOptionsBuilder<PersistencePostgresContext>();
optionsBuilder.UseNpgsql(connectionString);
var context = new PersistencePostgresContext(optionsBuilder.Options);
return context;
}
}