Skip to content

Commit 7de628f

Browse files
committed
switching between SQLite and Postgres
1 parent e53f9d9 commit 7de628f

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,7 @@ MigrationBackup/
348348

349349
# Ionide (cross platform F# VS Code tools) working folder
350350
.ionide/
351+
352+
# Database
353+
*.db
354+
*.db-*

EntityFrameworkExample/Program.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,22 @@
99
builder.Services.AddEndpointsApiExplorer();
1010
builder.Services.AddSwaggerGen();
1111

12-
builder.Services.AddDbContext<EntityContext>(
13-
options => options.UseNpgsql(builder.Configuration.GetConnectionString("EntityDb"))
14-
);
12+
String connectionString = Environment.GetEnvironmentVariable("DB");
13+
14+
if(connectionString == "sqlite") {
15+
Console.WriteLine($"Using database connection {connectionString}");
16+
builder.Services.AddDbContext<EntityContext>(
17+
options => options.UseSqlite(builder.Configuration.GetConnectionString(connectionString))
18+
);
19+
} else {
20+
connectionString = "postgres";
21+
Console.WriteLine($"Using database connection {connectionString}");
22+
23+
builder.Services.AddDbContext<EntityContext>(
24+
options => options.UseNpgsql(builder.Configuration.GetConnectionString(connectionString))
25+
);
26+
}
27+
1528

1629
var app = builder.Build();
1730

EntityFrameworkExample/appsettings.Development.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
}
77
},
88
"ConnectionStrings": {
9-
"EntityDb": "Server=localhost;Database=entity-framework-example;User Id=user;Password=pass"
9+
"postgres": "Server=localhost;Database=entity-framework-example;User Id=user;Password=pass",
10+
"sqlite": "Data Source=entity.db"
1011
}
1112
}

docker-compose.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ services:
2424
restart: unless-stopped
2525
ports:
2626
- ${ADMINER_PORT}:8080
27-
27+
2828
sqlitebrowser:
2929
image: "lscr.io/linuxserver/sqlitebrowser:${SQLITE_BROWSER_TAG}"
3030
container_name: "${PROJECT_NAME}-sqlitebrowser"
3131
security_opt:
3232
- seccomp:unconfined
33-
ports:
34-
- ${SQLITE_BROWSER_PORT}:3000
3533
stop_grace_period: 5s
3634
restart: unless-stopped
35+
ports:
36+
- ${SQLITE_BROWSER_PORT}:3000
37+
volumes:
38+
- ./EntityFrameworkExample:/app
3739

3840
networks:
3941
default:

0 commit comments

Comments
 (0)