Exemple #1
0
 private void GetDbContextOptions <TContext>(DbContextOptionsBuilder <TContext> optionsBuilder, string connectionString, int?DBType, string schemaName) where TContext : DbContext
 {
     if (DBType == (int)DBTypes.MSSQL)
     {
         optionsBuilder.UseSqlServer(connectionString,
                                     sqlServerOptionsAction: sqlOptions =>
         {
             sqlOptions.MigrationsAssembly(typeof(SampleAppSettings).GetTypeInfo().Assembly.GetName().Name);
             sqlOptions.EnableRetryOnFailure(maxRetryCount: _sampleAppSettings.DbSettings.MaxRetryCount <= 0 ? 5 : _sampleAppSettings.DbSettings.MaxRetryCount, maxRetryDelay: TimeSpan.FromSeconds(_sampleAppSettings.DbSettings.MaxRetryDelay <= 0 ? 30 : _sampleAppSettings.DbSettings.MaxRetryDelay), errorNumbersToAdd: null);
         }).ReplaceService <IModelCacheKeyFactory, SampleAppDbContextFactory>();;
     }
     else if (DBType == (int)DBTypes.PostGreSQL)
     {
         optionsBuilder.UseNpgsql(connectionString, npgSqlOptions =>
         {
             npgSqlOptions.MigrationsAssembly(typeof(SampleAppSettings).GetTypeInfo().Assembly.GetName().Name);
             npgSqlOptions.EnableRetryOnFailure(maxRetryCount: _sampleAppSettings.DbSettings.MaxRetryCount <= 0 ? 5 : _sampleAppSettings.DbSettings.MaxRetryCount, maxRetryDelay: TimeSpan.FromSeconds(_sampleAppSettings.DbSettings.MaxRetryDelay <= 0 ? 30 : _sampleAppSettings.DbSettings.MaxRetryDelay), errorCodesToAdd: null);
         }).ReplaceService <IModelCacheKeyFactory, SampleAppDbContextFactory>();;
     }
     else
     {
         NLogger.Instance().Error($"DBtype='{DBType}' is not supported  for tenant={ TenantHelper.GetCurrentTenantId(_sampleAppSettings) }");
     }
 }
Exemple #2
0
        private SampleAppBaseContext ResolveDbContext(IServiceProvider provider)
        {
            var tenantSettingsList = TenantHelper.GetTenantAppSettingsList(_sampleAppSettings, TenantHelper.GetCurrentTenantId(_sampleAppSettings));

            if (tenantSettingsList.NotAssigned() || ((JArray)tenantSettingsList).Count == 0)
            {
                throw new Exception($"Belirtilen kiracı için uygulama ayarları yapılmamıştır: { HttpContextExtensions.GetCurrentContextId()}");
            }

            SampleAppBaseContext dbContext = new SampleAppBaseContext();
            var    tenantAppSettings       = ((JArray)tenantSettingsList)[0];
            string serverAddr     = tenantAppSettings["ServerAddress"].ToString();
            int    dbType         = tenantAppSettings["DBType"].ToInt();
            string dbName         = tenantAppSettings["DBName"].ToString();
            int?   port           = tenantAppSettings["Port"].ToInt();
            string serverUsername = tenantAppSettings["ServerUsername"].ToString();
            string serverPassword = tenantAppSettings["ServerPassword"].ToString();
            string schemaName     = tenantAppSettings["SchemaName"].ToString();

            if (dbType == (int)DBTypes.MSSQL)
            {
                DbContextOptionsBuilder <MsSqlContext> sqlOptionsBuilder = new DbContextOptionsBuilder <MsSqlContext>();
                GetDbContextOptions(sqlOptionsBuilder, $"Data Source={serverAddr},{port};Initial Catalog={dbName};User Id={serverUsername};Password={serverPassword};", dbType, schemaName);
                dbContext = new MsSqlContext(sqlOptionsBuilder.Options);
            }
            else if (dbType == (int)DBTypes.PostGreSQL)
            {
                DbContextOptionsBuilder <PostgreSqlContext> sqlOptionsBuilder = new DbContextOptionsBuilder <PostgreSqlContext>();
                GetDbContextOptions(sqlOptionsBuilder, $"Host = {serverAddr}; Database = {dbName}; User ID = {serverUsername}; Password = {serverPassword}; Port = {port};Pooling = true;", dbType, schemaName);
                dbContext = new PostgreSqlContext(sqlOptionsBuilder.Options);
            }

            dbContext.SchemaName = schemaName;
            dbContext.DbType     = (DBTypes)dbType;

            var dbInitializer = new DbInitializer(dbContext);

            dbInitializer.Initialize();

            return(dbContext);
        }