Created
January 17, 2024 14:01
-
-
Save esersahin/5adecc35b39b32725e5a6cc219e16e3f to your computer and use it in GitHub Desktop.
ConnectionSelectors.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do | |
{ | |
Console.WriteLine("Which database do you want to use? (1 for SQL Server, 2 for PostgreSQL, 3 for Sqlite)"); | |
var readKey = Console.ReadKey(); | |
Console.WriteLine(); | |
Console.WriteLine(); | |
if (readKey.KeyChar == '1') | |
{ | |
ConnectionStrings.Current = ConnectionStrings.SqlServer; | |
break; | |
} | |
if (readKey.KeyChar == '2') | |
{ | |
ConnectionStrings.Current = ConnectionStrings.PostgreSql; | |
break; | |
} | |
if (readKey.KeyChar == '3') | |
{ | |
ConnectionStrings.Current = ConnectionStrings.Sqlite; | |
break; | |
} | |
Console.WriteLine("Please select a valid option."); | |
} while (true); | |
using var context = new AppDbContext(); | |
context.Database.EnsureDeleted(); | |
context.Database.EnsureCreated(); | |
public static class ConnectionStrings | |
{ | |
public static string SqlServer = ""; | |
public static string PostgreSql = ""; | |
public static string Sqlite = "Data Source=ConcurrencyProductDb.db"; | |
public static string Current { get; set; } | |
} | |
public class AppDbContext : DbContext | |
{ | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
if (ConnectionStrings.Current == ConnectionStrings.SqlServer) | |
{ | |
optionsBuilder | |
.UseSqlServer(ConnectionStrings.SqlServer) | |
.LogTo(Console.WriteLine, LogLevel.Information) | |
.EnableSensitiveDataLogging(); | |
} | |
else (ConnectionStrings.Current == ConnectionStrings.SqlServer) | |
{ | |
optionsBuilder | |
.UseNpgsql(ConnectionStrings.PostgreSql) | |
.LogTo(Console.WriteLine, LogLevel.Information) | |
.EnableSensitiveDataLogging(); | |
} | |
else | |
{ | |
optionsBuilder | |
.UseSqlite(ConnectionStrings.Sqlite) | |
.LogTo(Console.WriteLine, LogLevel.Information) | |
.EnableSensitiveDataLogging(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment