Ejemplo n.º 1
0
        public IDbAccessor GetDbAccessor(DbContextParamters dbContextParamters, string optionName = null)
        {
            EFCoreShardingOptions eFCoreShardingOptions = _optionsMonitor.BuildOption(optionName);

            var dbContext = GetDbContext(dbContextParamters, eFCoreShardingOptions);

            return(GetProvider(dbContextParamters.DbType).GetDbAccessor(dbContext));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 4
0
        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));
        }
Ejemplo n.º 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
            {
            }
        }
Ejemplo n.º 6
0
        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));
        }