public SchoolContext Create(DbContextFactoryOptions options, ConnectionStringSelector connectionStringSelector) { var config = Config.GetInstance(); var dbContextOptionsBuilder = new DbContextOptionsBuilder <SchoolContext>(); switch (config.DatabaseType) { case Config.Database.MySql: dbContextOptionsBuilder .UseMySQL(config.MySqlDatabaseConfig.GetConnectionString(connectionStringSelector)); break; case Config.Database.SqlServer: dbContextOptionsBuilder .UseSqlServer(config.SqlServerDatabaseConfig.GetConnectionString(connectionStringSelector)); break; case Config.Database.PostgreSql: dbContextOptionsBuilder .UseNpgsql(config.PostgreSqlDatabaseConfig.GetConnectionString(connectionStringSelector)); break; default: throw new Exception($"Unknown database type (was '{config.DatabaseType}')"); } dbContextOptionsBuilder.UseLazyLoading(); var ctx = new SchoolContext(dbContextOptionsBuilder.Options); return(ctx); }
public void GetConnectionStringName_Facility1_ReturnsAVPM() { // Arrange string namespaceName = "AVPM"; string facility = "1"; string expected = "ODBCAVPM"; // Act string actual = ConnectionStringSelector.GetConnectionStringName(namespaceName, facility); // Assert Assert.AreEqual(expected, actual); }
public string GetConnectionString(ConnectionStringSelector connectionStringSelector) { switch (connectionStringSelector) { case ConnectionStringSelector.Main: return(MainConnectionString); case ConnectionStringSelector.Second: return(SecondConnectionString); default: throw new Exception($"Unknown {typeof(ConnectionStringSelector)} value (was {connectionStringSelector})."); } }
protected SchoolContext CreateDbContext(ConnectionStringSelector connectionStringSelector, ContextInitializationOptions options) { SchoolContext ctx; if (options == ContextInitializationOptions.CleanupData || options == ContextInitializationOptions.SeedSampleData) { using (ctx = _ctxFactory.Create(new DbContextFactoryOptions(), connectionStringSelector)) { ctx.Database.EnsureDeleted(); if (options == ContextInitializationOptions.SeedSampleData) { DbInitializer.Initialize(ctx); } } } ctx = _ctxFactory.Create(new DbContextFactoryOptions(), connectionStringSelector); ctx.Database.EnsureCreated(); ctx.Database.Migrate(); return(ctx); }