Esempio n. 1
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);
        }