Skip to content

Instantly share code, notes, and snippets.

@andrewlock
Last active August 4, 2016 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewlock/d2caf49b19e3a9328ccc3e85c29e11a2 to your computer and use it in GitHub Desktop.
Save andrewlock/d2caf49b19e3a9328ccc3e85c29e11a2 to your computer and use it in GitHub Desktop.
Creating DbContext ConfigureServices
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