public IDbAccessor GetDbAccessor(DbContextParamters dbContextParamters, string optionName = null)
        {
            EFCoreShardingOptions eFCoreShardingOptions = _optionsMonitor.BuildOption(optionName);

            var dbContext = GetDbContext(dbContextParamters, eFCoreShardingOptions);

            return(GetProvider(dbContextParamters.DbType).GetDbAccessor(dbContext));
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="contextOptions"></param>
        /// <param name="paramter"></param>
        /// <param name="shardingOptions"></param>
        public GenericDbContext(DbContextOptions contextOptions, DbContextParamters paramter, EFCoreShardingOptions shardingOptions)
            : base(contextOptions)
        {
            DbContextOption = contextOptions;
            Paramter        = paramter;
            ShardingOption  = shardingOptions;

            Database.SetCommandTimeout(ShardingOption.CommandTimeout);
        }
Example #3
0
        public IDbAccessor GetDbAccessor(string conString, DatabaseType dbType, string entityNamespace = null, string suffix = null)
        {
            DbContextParamters options = new DbContextParamters
            {
                ConnectionString = conString,
                DbType           = dbType,
                EntityNamespace  = entityNamespace,
                Suffix           = suffix,
            };

            var dbContext = GetDbContext(options);

            return(GetProvider(dbType).GetDbAccessor(dbContext));
        }
        public GenericDbContext GetDbContext(DbContextParamters options)
        {
            AbstractProvider provider = GetProvider(options.DbType);

            DbConnection dbConnection = provider.GetDbConnection();

            dbConnection.ConnectionString = options.ConnectionString;

            DbContextOptionsBuilder builder = new DbContextOptionsBuilder();

            builder.UseLoggerFactory(_loggerFactory);

            provider.UseDatabase(builder, dbConnection);
            builder.ReplaceService <IModelCacheKeyFactory, GenericModelCacheKeyFactory>();
            builder.ReplaceService <IMigrationsModelDiffer, ShardingMigration>();

            return(new GenericDbContext(builder.Options, options, _shardingOptions));
        }
Example #5
0
        public void CreateTable(string conString, DatabaseType dbType, Type entityType, string suffix)
        {
            DbContextParamters options = new DbContextParamters
            {
                ConnectionString = conString,
                DbType           = dbType,
                EntityTypes      = new Type[] { entityType },
                Suffix           = suffix
            };

            using DbContext dbContext = GetDbContext(options);
            var databaseCreator = dbContext.Database.GetService <IDatabaseCreator>() as RelationalDatabaseCreator;

            try
            {
                databaseCreator.CreateTables();
            }
            catch
            {
            }
        }
        public GenericDbContext GetDbContext(DbContextParamters dbContextParamters, EFCoreShardingOptions eFCoreShardingOptions)
        {
            if (eFCoreShardingOptions == null)
            {
                eFCoreShardingOptions = _optionsMonitor.BuildOption(null);
            }

            AbstractProvider provider = GetProvider(dbContextParamters.DbType);

            DbConnection dbConnection = provider.GetDbConnection();

            dbConnection.ConnectionString = dbContextParamters.ConnectionString;

            DbContextOptionsBuilder builder = new DbContextOptionsBuilder();

            builder.UseLoggerFactory(_loggerFactory);

            provider.UseDatabase(builder, dbConnection);
            builder.ReplaceService <IModelCacheKeyFactory, GenericModelCacheKeyFactory>();
            builder.ReplaceService <IMigrationsModelDiffer, ShardingMigration>();

            return(new GenericDbContext(builder.Options, dbContextParamters, eFCoreShardingOptions));
        }