示例#1
0
        public MySqlDbContextOptionsProvider(MySqlOptions mySqlOptions, ILoggerFactory loggerFactory)
        {
            _connection       = new MySqlConnection(mySqlOptions.ConnectionString);
            _dbContextOptions = new DbContextOptionsBuilder()
#if EFCORE5
                                .UseMySql(_connection, mySqlOptions.ServerVersion, mySqlOptions.MySqlOptionsAction)
#endif
#if !EFCORE5
                                .UseMySql(_connection, mySqlOptions.MySqlOptionsAction)
#endif
                                .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
                                .UseLoggerFactory(loggerFactory)
                                .ReplaceService <IQueryCompiler, ShardingQueryCompiler>()
                                .ReplaceService <IModelCacheKeyFactory, ShardingModelCacheKeyFactory>()
                                .UseShardingSqlServerQuerySqlGenerator()
                                .Options;
        }
示例#2
0
        public static IServiceCollection AddShardingMySql(this IServiceCollection services, Action <MySqlOptions> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException($"AddScfSqlServerProvider :{nameof(configure)}");
            }

            var options = new MySqlOptions();

            configure(options);
            services.AddSingleton(options);
            services.AddShardingCore();

            services.AddScoped <IDbContextOptionsProvider, MySqlDbContextOptionsProvider>();
            services.AddSingleton <IShardingParallelDbContextFactory, ShardingMySqlParallelDbContextFactory>();
            if (options.HasSharding)
            {
                foreach (var shardingRoute in options.ShardingRoutes)
                {
                    var genericVirtualRoute = shardingRoute.GetInterfaces().FirstOrDefault(it => it.IsInterface && it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IVirtualRoute <>) &&
                                                                                           it.GetGenericArguments().Any());
                    if (genericVirtualRoute == null)
                    {
                        throw new ArgumentException("add sharding route type error not assignable from IVirtualRoute<>.");
                    }
                    var shardingEntity = genericVirtualRoute.GetGenericArguments()[0];
                    if (!shardingEntity.IsShardingEntity())
                    {
                        throw new ArgumentException("add sharding route type error generic arguments first not assignable from IShardingEntity.");
                    }
                    Type genericType   = typeof(IVirtualRoute <>);
                    Type interfaceType = genericType.MakeGenericType(shardingEntity);
                    services.AddSingleton(interfaceType, shardingRoute);
                }
            }
            services.AddSingleton(sp =>
            {
                var shardingCoreConfig = new ShardingCoreConfig();
                options.ShardingCoreConfigConfigure?.Invoke(sp, shardingCoreConfig);
                return(shardingCoreConfig);
            });
            services.AddSingleton <IShardingBootstrapper, ShardingBootstrapper>();
            return(services);
        }
 public ShardingMySqlParallelDbContextFactory(IVirtualTableManager virtualTableManager, MySqlOptions mySqlOptions)
 {
     _virtualTableManager = virtualTableManager;
     _mySqlOptions        = mySqlOptions;
 }