using System.Diagnostics; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.Extensions.Caching.Memory; using Sample.MySql.DbContexts; using Sample.MySql.Domain.Entities; using Sample.MySql.multi; using Sample.MySql.Shardings; using ShardingCore; using ShardingCore.Bootstrappers; using ShardingCore.Core; using ShardingCore.Core.ModelCacheLockerProviders; using ShardingCore.Core.RuntimeContexts; using ShardingCore.DynamicDataSources; using ShardingCore.EFCores; using ShardingCore.Extensions; using ShardingCore.Helpers; using ShardingCore.Sharding.ParallelTables; using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.TableExists; using ShardingCore.TableExists.Abstractions; namespace Sample.MySql { // public class AutoStart : IHostedService // { // // public AutoStart(IShardingBootstrapper shardingBootstrapper) // { // shardingBootstrapper.Start(); // } // public Task StartAsync(CancellationToken cancellationToken) // { // return Task.CompletedTask; // } // // public Task StopAsync(CancellationToken cancellationToken) // { // return Task.CompletedTask; // } // } // public interface ITenant // { // string GetTenantId(); // } // // public class DefaultTenant :ITenant // { // private readonly string _tenantId; // // public DefaultTenant(string tenantId) // { // _tenantId = tenantId; // } // public string GetTenantId() // { // return _tenantId; // } // } public class Startup { public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder => { builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole(); }); public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // services.AddHostedService(); services.AddControllers(); services.AddSingleton(sp => new MemoryCache(new MemoryCacheOptions { SizeLimit = 102400 })); // // Action optionsBuilder = null; // services.AddDbContext(optionsBuilder); // services.AddDbContext((sp,builder) => // { // optionsBuilder(sp, builder); // }); // var type = typeof(MyUserRoute); services.AddDbContext(o => o.UseMySql( "server=127.0.0.1;port=3306;database=dbdbdxx;userid=root;password=root;",new MySqlServerVersion( new Version()))); services.AddShardingDbContext() .UseRouteConfig((sp, o) => { // AppDomain.CurrentDomain.GetAssemblies(). o.AddShardingTableRoute(); o.AddShardingTableRoute(); o.AddShardingTableRoute(); o.AddShardingDataSourceRoute(); o.AddShardingTableRoute(); o.AddShardingTableRoute(); o.AddShardingTableRoute(); o.AddParallelTableGroupNode(new ParallelTableGroupNode(new List() { new ParallelTableComparerType(typeof(TestMod)), new ParallelTableComparerType(typeof(TestModItem)), })); }).UseConfig((sp, o) => { // var memoryCache = sp.ApplicationServiceProvider.GetRequiredService(); // o.UseExecutorDbContextConfigure(b => // { // b.UseMemoryCache(memoryCache); // }); o.CheckShardingKeyValueGenerated = false; o.IgnoreCreateTableError = false; o.UseEntityFrameworkCoreProxies = true; o.CacheModelLockConcurrencyLevel = 1024; o.CacheEntrySize = 1; o.CacheModelLockObjectSeconds = 10; o.CheckShardingKeyValueGenerated = false; var loggerFactory1 = sp.GetService(); var loggerFactory2 = sp.ApplicationServiceProvider.GetService(); // o.UseEntityFrameworkCoreProxies = true; o.ThrowIfQueryRouteNotMatch = false; o.AutoUseWriteConnectionStringAfterWriteDb = true; o.MaxQueryConnectionsLimit = 10; o.UseExecutorDbContextConfigure(op => { }); o.UseShardingQuery((conStr, builder) => { // var logger = sp.ApplicationServiceProvider.GetService>(); // logger.LogInformation(conStr); builder.UseMySql(conStr, new MySqlServerVersion(new Version())); // .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) // .UseLoggerFactory(loggerFactory1) // .EnableSensitiveDataLogging(); //.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }); o.UseShardingTransaction((connection, builder) => { builder .UseMySql(connection, new MySqlServerVersion(new Version())); // .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) // .UseLoggerFactory(loggerFactory1) // .EnableSensitiveDataLogging(); //.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }); o.UseExecutorDbContextConfigure(builder => { builder .AddInterceptors(new MySaveChangeInterceptor()); }); o.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;"); o.AddExtraDataSource(sp => new Dictionary() { { "ds1", "server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;" }, { "ds2", "server=127.0.0.1;port=3306;database=dbdbd2;userid=root;password=root;" } }); o.UseShardingMigrationConfigure(b => { b.ReplaceService(); }); }).ReplaceService() .ReplaceService() .AddShardingCore(); services.AddScoped(); // services.AddDbContext(ShardingCoreExtension // .UseMutliDefaultSharding); // services.AddShardingDbContext() // .AddEntityConfig(o => // { // o.CreateDataBaseOnlyOnStart = true; // o.CreateShardingTableOnStart = true; // o.EnsureCreatedWithOutShardingTable = true; // o.IgnoreCreateTableError = true; // o.AddShardingTableRoute(); // o.AddShardingTableRoute(); // o.AddShardingDataSourceRoute(); // o.UseShardingQuery((conStr, builder) => // { // builder.UseMySql(conStr, new MySqlServerVersion(new Version()) // ,b=>b.EnableRetryOnFailure() // ) // .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking).UseLoggerFactory(efLogger); // //builder.UseMySql(conStr, new MySqlServerVersion(new Version())); // }); // o.UseShardingTransaction((connection, builder) => // { // builder.UseMySql(connection, new MySqlServerVersion(new Version()) // ,b=>b.EnableRetryOnFailure() // ) // .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking).UseLoggerFactory(efLogger); // }); // }) // .AddConfig(op => // { // op.ConfigId = "c0"; // op.AddDefaultDataSource("ds0", // "server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;"); // // //op.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=db2;userid=root;password=L6yBtV6qNENrwBy7;") // op.ReplaceTableEnsureManager(sp=>new MySqlTableEnsureManager()); // }).EnsureConfig(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.ApplicationServices.UseAutoTryCompensateTable(); using (var scope = app.ApplicationServices.CreateScope()) { var unShardingDbContext = scope.ServiceProvider.GetService(); unShardingDbContext.Database.EnsureCreated(); // var shardingRuntimeContext = defaultShardingDbContext.GetShardingRuntimeContext(); // var tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); // var virtualTableRoute = (SysUserLogByMonthRoute)tableRouteManager.GetRoute(typeof(SysUserLogByMonth)); // virtualTableRoute.Append("2021"); } // var shardingRuntimeContext = app.ApplicationServices.GetRequiredService(); // var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); // var entityMetadata = entityMetadataManager.TryGet(); // using (var scope = app.ApplicationServices.CreateScope()) // { // var defaultShardingDbContext = scope.ServiceProvider.GetService(); // // if (defaultShardingDbContext.Database.GetPendingMigrations().Any()) // { // defaultShardingDbContext.Database.GenerateCreateScript(); // } // } // app.ApplicationServices.UseAutoTryCompensateTable(); // using (var scope = app.ApplicationServices.CreateScope()) // { // var defaultShardingDbContext = scope.ServiceProvider.GetService(); // // if (defaultShardingDbContext.Database.GetPendingMigrations().Any()) // { // try // { // // defaultShardingDbContext.Database.Migrate(); // } // catch (Exception e) // { // } // } // // var shardingRuntimeContext = defaultShardingDbContext.GetShardingRuntimeContext(); // // var tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); // // var virtualTableRoute = (SysUserLogByMonthRoute)tableRouteManager.GetRoute(typeof(SysUserLogByMonth)); // // virtualTableRoute.Append("2021"); // } // // app.ApplicationServices.UseAutoTryCompensateTable(12); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); // for (int i = 1; i < 500; i++) // { // using (var conn = new MySqlConnection( // $"server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;")) // { // conn.Open(); // } // DynamicShardingHelper.DynamicAppendDataSource($"c0",$"ds{i}",$"server=127.0.0.1;port=3306;database=dbdbd{i};userid=root;password=root;"); // // } // using (var scope = app.ApplicationServices.CreateScope()) // { // var defaultShardingDbContext = scope.ServiceProvider.GetRequiredService(); // var addMonths = DateTime.Now.AddMonths(-5); // defaultShardingDbContext.Set().Where(o => o.Time >= addMonths).Any(); // } app.DbSeed(); } } }