Last active
August 4, 2016 17:11
-
-
Save andrewlock/d2caf49b19e3a9328ccc3e85c29e11a2 to your computer and use it in GitHub Desktop.
Creating DbContext ConfigureServices
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
public void ConfigureServices(IServiceCollection services) | |
{ | |
var connectionString = Configuration["ApplicationDbContext:ConnectionString"]; | |
services.AddDbContext<ApplicationDbContext>( | |
opts => opts.UseNpgsql(connectionString) | |
); | |
services.Configure<MultitenancyOptions>( | |
options => | |
{ | |
var scopeFactory = services | |
.BuildServiceProvider() | |
.GetRequiredService<IServiceScopeFactory>(); | |
using (var scope = scopeFactory.CreateScope()) | |
{ | |
var provider = scope.ServiceProvider; | |
using (var dbContext = provider.GetRequiredService<ApplicationDbContext>()) | |
{ | |
options.AppTenants = dbContext.AppTenants.ToList(); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment