41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
|
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<PersistenceDbContext>(options => options.UseNpgsql(connectionString),
|
||
|
ServiceLifetime.Transient);
|
||
|
|
||
|
serviceProvider = serviceCollection.BuildServiceProvider();
|
||
|
|
||
|
var context = serviceProvider.GetRequiredService<PersistenceDbContext>();
|
||
|
context.Database.EnsureCreated();
|
||
|
context.Database.AddPartitioning();
|
||
|
context.SaveChanges();
|
||
|
}
|
||
|
|
||
|
public void Dispose()
|
||
|
{
|
||
|
var dbContext = new PersistencePostgresContext(
|
||
|
new DbContextOptionsBuilder<PersistencePostgresContext>()
|
||
|
.UseNpgsql(connectionString)
|
||
|
.Options);
|
||
|
|
||
|
dbContext.Database.EnsureDeleted();
|
||
|
|
||
|
GC.SuppressFinalize(this);
|
||
|
}
|
||
|
}
|