using System; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using TestUtils.DataAccess; namespace TestUtils { public class Tests { public const string ConnectionString = "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;"; public static void TestRealDb(Action func) { IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddDbContext(builder => builder.UseNpgsql(ConnectionString), ServiceLifetime.Transient); var serviceProvider = serviceCollection.BuildServiceProvider(); var dbContext = (LuUtilsDbContext)serviceProvider.GetService(typeof(LuUtilsDbContext)); try { func(serviceProvider); } finally { dbContext.pk_bigserial.RemoveRange(dbContext.pk_bigserial); dbContext.pk_guid.RemoveRange(dbContext.pk_guid); dbContext.SaveChanges(); dbContext.Dispose(); } } } }