protected static async Task ExecuteBeerIdentityContextAwareTest(Func <DbContextOptionsBuilder <BeerIdentityContext>, BeerIdentityContext, Task> executor) { Random random1 = new Random(); var config = ConfigurationUtility.GetConfig(); String connectionString = $"{ConfigurationUtility.GetConnectionString("PostgresTest")};Database={random1.GetAlphanumericString()};"; DbContextOptionsBuilder <BeerIdentityContext> dbContextOptionsBuilder = new(); dbContextOptionsBuilder.UseNpgsql(connectionString, (ib) => ib.MigrationsAssembly(typeof(BeerIdentityContext).Assembly.FullName)); DbContextOptions <BeerIdentityContext> contextOptions = dbContextOptionsBuilder.Options; BeerIdentityContext context = new BeerIdentityContext(contextOptions); try { await context.Database.MigrateAsync(); await executor(dbContextOptionsBuilder, context); } finally { await context.Database.EnsureDeletedAsync(); } }
/// <summary> /// Attempts to first read a connection string from the connectionStrings configuration section. /// If not found there, it will attempt to read from environment variables. /// </summary> /// <param name="connectionStringName">The name of the connection string to look up.</param> /// <returns>The connection string, or <see langword="null"/> if no connection string was found.</returns> public string GetConnectionString(string connectionStringName) { // first try prefixing string prefixedConnectionStringName = GetPrefixedConnectionStringName(connectionStringName); string connectionString = ConfigurationUtility.GetConnectionString(prefixedConnectionStringName); if (string.IsNullOrEmpty(connectionString)) { // next try a direct unprefixed lookup connectionString = ConfigurationUtility.GetConnectionString(connectionStringName); } return(connectionString); }
private static string GetConnectionString(string connectionStringName) => ConfigurationUtility.GetConnectionString(connectionStringName);