Last active
November 4, 2018 20:38
-
-
Save Compufreak345/eb6bb274a5a49cbbd755b6981d2399b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ConfigureServices | |
var connectionStringProvider = new ConnectionStringProvider<WebApiDbContext>("ConnectionString"); | |
services.AddSingleton(connectionStringProvider); | |
services.AddDbContext<WebApiDbContext>((s, o) => o.UseSqlite( | |
s.GetService<ConnectionStringProvider<WebApiDbContext>>().ConnectionString)); | |
// Singleton provider for connection string (or other DbContextOptions) | |
public class ConnectionStringProvider<TContext> where TContext : DbContext | |
{ | |
public string ConnectionString { get; set; } | |
public ConnectionStringProvider(string connectionString) | |
{ | |
this.ConnectionString = connectionString; | |
} | |
} | |
// Now you can just get the ConnectionStringProvider from your DI-Chain and swap the connection string at runtime, | |
// with effect at the next scope (request). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment