Skip to content

Instantly share code, notes, and snippets.

@gavilanch
Last active June 24, 2021 18:46
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 gavilanch/f52699432e425a02b1128e1e09bf0d96 to your computer and use it in GitHub Desktop.
Save gavilanch/f52699432e425a02b1128e1e09bf0d96 to your computer and use it in GitHub Desktop.
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddDbContext<ApplicationDbContext>((serviceProvider, dbContextBuilder) =>
{
var connectionStringPlaceHolder = Configuration.GetConnectionString("PlaceHolderConnection");
var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
var dbName = httpContextAccessor.HttpContext.Request.Headers["tenantId"].First();
var connectionString = connectionStringPlaceHolder.Replace("{dbName}", dbName);
dbContextBuilder.UseSqlServer(connectionString);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment