修改demo移除不需要的代码

This commit is contained in:
xuejiaming 2022-09-19 10:42:10 +08:00
parent 716508cbdf
commit a4f70cea61
3 changed files with 62 additions and 95 deletions

View File

@ -1,18 +1,8 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Sample.MySql.DbContexts; using Sample.MySql.DbContexts;
using Sample.MySql.Domain.Entities; using Sample.MySql.Domain.Entities;
using Sample.MySql.multi; using Sample.MySql.multi;
using ShardingCore.Extensions.ShardingQueryableExtensions;
using ShardingCore.TableCreator;
namespace Sample.MySql.Controllers namespace Sample.MySql.Controllers
{ {
@ -27,12 +17,10 @@ namespace Sample.MySql.Controllers
{ {
private readonly DefaultShardingDbContext _defaultTableDbContext; private readonly DefaultShardingDbContext _defaultTableDbContext;
private readonly OtherDbContext _otherDbContext;
public WeatherForecastController(DefaultShardingDbContext defaultTableDbContext,OtherDbContext otherDbContext) public WeatherForecastController(DefaultShardingDbContext defaultTableDbContext)
{ {
_defaultTableDbContext = defaultTableDbContext; _defaultTableDbContext = defaultTableDbContext;
_otherDbContext = otherDbContext;
} }
public IQueryable<SysTest> GetAll() public IQueryable<SysTest> GetAll()
@ -72,7 +60,8 @@ namespace Sample.MySql.Controllers
var shardingFirstOrDefaultAsync = await _defaultTableDbContext.Set<SysUserLogByMonth>().ToListAsync(); var shardingFirstOrDefaultAsync = await _defaultTableDbContext.Set<SysUserLogByMonth>().ToListAsync();
var shardingCountAsync = await _defaultTableDbContext.Set<SysUserMod>().CountAsync(); var shardingCountAsync = await _defaultTableDbContext.Set<SysUserMod>().CountAsync();
var shardingCountAsyn2c = _defaultTableDbContext.Set<SysUserLogByMonth>().Count(); var shardingCountAsyn2c = _defaultTableDbContext.Set<SysUserLogByMonth>().Count();
var count = _otherDbContext.Set<MyUser>().Count();
// var dbConnection = _defaultTableDbContext.Database.GetDbConnection(); // var dbConnection = _defaultTableDbContext.Database.GetDbConnection();
// if (dbConnection.State != ConnectionState.Open) // if (dbConnection.State != ConnectionState.Open)
// { // {

View File

@ -23,8 +23,6 @@ namespace Sample.MySql
{ {
using (var scope=app.ApplicationServices.CreateScope()) using (var scope=app.ApplicationServices.CreateScope())
{ {
var otherDbContext =scope.ServiceProvider.GetService<OtherDbContext>();
var any = otherDbContext.MyUsers.Any();
var virtualDbContext =scope.ServiceProvider.GetService<DefaultShardingDbContext>(); var virtualDbContext =scope.ServiceProvider.GetService<DefaultShardingDbContext>();
if (!virtualDbContext.Set<SysUserMod>().Any()) if (!virtualDbContext.Set<SysUserMod>().Any())
{ {

View File

@ -65,14 +65,46 @@ namespace Sample.MySql
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>(); // op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>(); // op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
// }); // });
services.AddMultiShardingDbContext<OtherDbContext>() // services.AddMultiShardingDbContext<OtherDbContext>()
.UseRouteConfig(op => // .UseRouteConfig(op =>
// {
// op.AddShardingTableRoute<MyUserRoute>();
// })
// .UseConfig((sp,o) =>
// {
// o.ThrowIfQueryRouteNotMatch = false;
// o.UseShardingQuery((conStr, builder) =>
// {
// builder.UseMySql(conStr, new MySqlServerVersion(new Version()))
// .UseLoggerFactory(efLogger)
// .EnableSensitiveDataLogging()
// .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
// });
// o.UseShardingTransaction((connection, builder) =>
// {
// builder
// .UseMySql(connection, new MySqlServerVersion(new Version()))
// .UseLoggerFactory(efLogger)
// .EnableSensitiveDataLogging()
// .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
// });
// o.UseShardingMigrationConfigure(b =>
// {
// b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
// });
// o.AddDefaultDataSource("ds0",
// "server=127.0.0.1;port=3306;database=dbdbdx;userid=root;password=root;");
// }).ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>().AddShardingCore();
services.AddShardingDbContext<DefaultShardingDbContext>()
.UseRouteConfig(o =>
{ {
op.AddShardingTableRoute<MyUserRoute>(); o.AddShardingTableRoute<SysUserLogByMonthRoute>();
}) o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
.UseConfig((sp,o) => o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
}).UseConfig(o =>
{ {
o.ThrowIfQueryRouteNotMatch = false; o.ThrowIfQueryRouteNotMatch = false;
o.AutoUseWriteConnectionStringAfterWriteDb = true;
o.UseShardingQuery((conStr, builder) => o.UseShardingQuery((conStr, builder) =>
{ {
builder.UseMySql(conStr, new MySqlServerVersion(new Version())) builder.UseMySql(conStr, new MySqlServerVersion(new Version()))
@ -88,61 +120,21 @@ namespace Sample.MySql
.EnableSensitiveDataLogging() .EnableSensitiveDataLogging()
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}); });
o.AddDefaultDataSource("ds0",
"server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;");
o.AddExtraDataSource(sp => new Dictionary<string, string>()
{
{ "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 => o.UseShardingMigrationConfigure(b =>
{ {
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>(); b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
}); });
o.AddDefaultDataSource("ds0", }).ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>()
"server=127.0.0.1;port=3306;database=dbdbdx;userid=root;password=root;"); .AddShardingCore();
}).ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>().AddShardingCore(); // services.AddDbContext<DefaultShardingDbContext>(ShardingCoreExtension
services.AddSingleton<IShardingRuntimeContext>(sp => // .UseMutliDefaultSharding<DefaultShardingDbContext>);
{
Stopwatch stopwatch = Stopwatch.StartNew();
var shardingRuntimeContext = new ShardingRuntimeBuilder<DefaultShardingDbContext>()
.UseRouteConfig(o =>
{
o.AddShardingTableRoute<SysUserLogByMonthRoute>();
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
}).UseConfig(o =>
{
o.ThrowIfQueryRouteNotMatch = false;
o.AutoUseWriteConnectionStringAfterWriteDb = true;
o.UseShardingQuery((conStr, builder) =>
{
builder.UseMySql(conStr, new MySqlServerVersion(new Version()))
.UseLoggerFactory(efLogger)
.EnableSensitiveDataLogging()
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});
o.UseShardingTransaction((connection, builder) =>
{
builder
.UseMySql(connection, new MySqlServerVersion(new Version()))
.UseLoggerFactory(efLogger)
.EnableSensitiveDataLogging()
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});
o.AddDefaultDataSource("ds0",
"server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;");
o.AddExtraDataSource(sp => new Dictionary<string, string>()
{
{ "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<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
});
}).ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>(ServiceLifetime.Singleton)
.Build(sp);
stopwatch.Stop();
Console.WriteLine("ShardingRuntimeContext build:" + stopwatch.ElapsedMilliseconds);
return shardingRuntimeContext;
});
services.AddDbContext<DefaultShardingDbContext>(ShardingCoreExtension
.UseMutliDefaultSharding<DefaultShardingDbContext>);
// services.AddShardingDbContext<DefaultShardingDbContext>() // services.AddShardingDbContext<DefaultShardingDbContext>()
// .AddEntityConfig(o => // .AddEntityConfig(o =>
// { // {
@ -187,33 +179,21 @@ namespace Sample.MySql
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }
app.ApplicationServices.UseAutoShardingCreate();
var shardingRuntimeContextManager = app.ApplicationServices.GetRequiredService<IShardingRuntimeContextManager>(); // app.ApplicationServices.UseAutoTryCompensateTable();
var shardingRuntimeContexts = shardingRuntimeContextManager.GetAll();
foreach (var keyValuePair in shardingRuntimeContexts)
{
keyValuePair.Value.UseAutoShardingCreate();
keyValuePair.Value.UseAutoTryCompensateTable();
}
// app.ApplicationServices.UseAutoShardingCreate(); // app.ApplicationServices.UseAutoShardingCreate();
// var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>(); // var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>();
// var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); // var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
// var entityMetadata = entityMetadataManager.TryGet<SysUserMod>(); // var entityMetadata = entityMetadataManager.TryGet<SysUserMod>();
// using (var scope = app.ApplicationServices.CreateScope()) using (var scope = app.ApplicationServices.CreateScope())
// { {
// var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>(); var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
// // if (defaultShardingDbContext.Database.GetPendingMigrations().Any()) // if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
// { {
// try defaultShardingDbContext.Database.Migrate();
// { }
// }
// defaultShardingDbContext.Database.Migrate();
// }
// catch (Exception e)
// {
// }
// }
// }
// using (var scope = app.ApplicationServices.CreateScope()) // using (var scope = app.ApplicationServices.CreateScope())
// { // {
// var defaultShardingDbContext = scope.ServiceProvider.GetService<OtherDbContext>(); // var defaultShardingDbContext = scope.ServiceProvider.GetService<OtherDbContext>();