using DD.Persistence.Database.Model; using DD.Persistence.Database.Postgres.Extensions; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace DD.Persistence.Database.Postgres.Test; public class DbFixture : IDisposable { public string connectionString { get; } public ServiceProvider serviceProvider { get; private set; } public DbFixture() { connectionString = $"Host=localhost;Port=5462;Username=postgres;Password=postgres;Database={Guid.CreateVersion7()}"; var serviceCollection = new ServiceCollection(); serviceCollection .AddDbContext(options => options.UseNpgsql(connectionString), ServiceLifetime.Transient); serviceProvider = serviceCollection.BuildServiceProvider(); var context = serviceProvider.GetRequiredService(); context.Database.EnsureCreated(); context.Database.AddPartitioning(); context.SaveChanges(); } public void Dispose() { var dbContext = new PersistencePostgresContext( new DbContextOptionsBuilder() .UseNpgsql(connectionString) .Options); dbContext.Database.EnsureDeleted(); GC.SuppressFinalize(this); } }