Skip to content

Instantly share code, notes, and snippets.

@misha130
Created February 4, 2020 15:25
Show Gist options
  • Save misha130/910508d3019d3d3bfa64dfb609df70f4 to your computer and use it in GitHub Desktop.
Save misha130/910508d3019d3d3bfa64dfb609df70f4 to your computer and use it in GitHub Desktop.
services.AddDbContext<ApplicationDbContext>((scope, options) =>
{
var context = scope.GetRequiredService<IActionContextAccessor>();
string connectionString = "DefaultConnection";
var actionContext = context.ActionContext;
if (actionContext != null)
{
var routeValues = actionContext.RouteData.Values;
if (routeValues.ContainsKey("community"))
{
connectionString = routeValues["community"].ToString();
}
}
options.UseNpgsql(
configuration.GetConnectionString(connectionString),
b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName));
});
@DoctaJonez
Copy link

DoctaJonez commented Feb 4, 2020

Looks good, but here's a slight nitpick...

connectionString = routeValues["community"].ToString();

Won't this part just get the community name / community id from the routing data?

It's not really a connection string, the variable name is a bit misleading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment