private static async Task ResetDatas <TEntity>(List <TEntity> entities) where TEntity : class { IConfiguration configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); var connectionString = configuration.GetConnectionString("LocalDatabase"); var options = new DbContextOptionsBuilder <TestExampleContext>() .UseMySql(configuration["ConnectionStrings:LocalDatabase"].ToString(), new MySqlServerVersion(new Version(1, 0, 0)), o => { o.MigrationsAssembly("TestExamples.API"); }) .Options; using (var context = new TestExampleContext(options)) { await context.Database.MigrateAsync().ConfigureAwait(false); var allEntities = await context.Set <TEntity>().ToListAsync().ConfigureAwait(false); context.Set <TEntity>().RemoveRange(allEntities); await context.SaveChangesAsync().ConfigureAwait(false); await context.Set <TEntity>().AddRangeAsync(entities).ConfigureAwait(false); await context.SaveChangesAsync().ConfigureAwait(false); } }
public BaseRepository(TestExampleContext testExampleContext) { _testExampleContext = testExampleContext; _dbSet = testExampleContext.Set <Tentity>(); }