Skip to content

Commit 94719ea

Browse files
committed
Use default ASP.NET location for connection strings
1 parent 72c0c09 commit 94719ea

File tree

7 files changed

+13
-13
lines changed

7 files changed

+13
-13
lines changed

src/Examples/DatabasePerTenantExample/Data/AppDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
3636
private string GetConnectionString()
3737
{
3838
string? tenantName = GetTenantName();
39-
string? connectionString = _configuration[$"Data:{tenantName ?? "Default"}Connection"];
39+
string? connectionString = _configuration.GetConnectionString(tenantName ?? "Default");
4040

4141
if (connectionString == null)
4242
{

src/Examples/DatabasePerTenantExample/appsettings.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"Data": {
3-
"DefaultConnection": "Host=localhost;Database=DefaultTenantDb;User ID=postgres;Password=###;Include Error Detail=true",
4-
"AdventureWorksConnection": "Host=localhost;Database=AdventureWorks;User ID=postgres;Password=###;Include Error Detail=true",
5-
"ContosoConnection": "Host=localhost;Database=Contoso;User ID=postgres;Password=###;Include Error Detail=true"
2+
"ConnectionStrings": {
3+
"Default": "Host=localhost;Database=DefaultTenantDb;User ID=postgres;Password=###;Include Error Detail=true",
4+
"AdventureWorks": "Host=localhost;Database=AdventureWorks;User ID=postgres;Password=###;Include Error Detail=true",
5+
"Contoso": "Host=localhost;Database=Contoso;User ID=postgres;Password=###;Include Error Detail=true"
66
},
77
"Logging": {
88
"LogLevel": {

src/Examples/JsonApiDotNetCoreExample/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ static void ConfigureServices(WebApplicationBuilder builder)
4848
builder.Services.AddDbContext<AppDbContext>(options =>
4949
{
5050
string? connectionString = GetConnectionString(builder.Configuration);
51-
5251
options.UseNpgsql(connectionString);
52+
5353
#if DEBUG
5454
options.EnableSensitiveDataLogging();
5555
options.EnableDetailedErrors();
@@ -76,7 +76,7 @@ static void ConfigureServices(WebApplicationBuilder builder)
7676
static string? GetConnectionString(IConfiguration configuration)
7777
{
7878
string postgresPassword = Environment.GetEnvironmentVariable("PGPASSWORD") ?? "postgres";
79-
return configuration["Data:DefaultConnection"]?.Replace("###", postgresPassword);
79+
return configuration.GetConnectionString("Default")?.Replace("###", postgresPassword);
8080
}
8181

8282
static void ConfigurePipeline(WebApplication webApplication)

src/Examples/JsonApiDotNetCoreExample/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"Data": {
3-
"DefaultConnection": "Host=localhost;Database=JsonApiDotNetCoreExample;User ID=postgres;Password=###;Include Error Detail=true"
2+
"ConnectionStrings": {
3+
"Default": "Host=localhost;Database=JsonApiDotNetCoreExample;User ID=postgres;Password=###;Include Error Detail=true"
44
},
55
"Logging": {
66
"LogLevel": {

src/Examples/NoEntityFrameworkExample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
static string? GetConnectionString(IConfiguration configuration)
3030
{
3131
string postgresPassword = Environment.GetEnvironmentVariable("PGPASSWORD") ?? "postgres";
32-
return configuration["Data:DefaultConnection"]?.Replace("###", postgresPassword);
32+
return configuration.GetConnectionString("Default")?.Replace("###", postgresPassword);
3333
}
3434

3535
static async Task CreateDatabaseAsync(IServiceProvider serviceProvider)

src/Examples/NoEntityFrameworkExample/Services/WorkItemService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public sealed class WorkItemService : IResourceService<WorkItem, int>
1616
public WorkItemService(IConfiguration configuration)
1717
{
1818
string postgresPassword = Environment.GetEnvironmentVariable("PGPASSWORD") ?? "postgres";
19-
_connectionString = configuration["Data:DefaultConnection"]?.Replace("###", postgresPassword);
19+
_connectionString = configuration.GetConnectionString("Default")?.Replace("###", postgresPassword);
2020
}
2121

2222
public async Task<IReadOnlyCollection<WorkItem>> GetAsync(CancellationToken cancellationToken)

src/Examples/NoEntityFrameworkExample/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"Data": {
3-
"DefaultConnection": "Host=localhost;Database=NoEntityFrameworkExample;User ID=postgres;Password=###;Include Error Detail=true"
2+
"ConnectionStrings": {
3+
"Default": "Host=localhost;Database=NoEntityFrameworkExample;User ID=postgres;Password=###;Include Error Detail=true"
44
},
55
"Logging": {
66
"LogLevel": {

0 commit comments

Comments
 (0)