修改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,41 +65,37 @@ 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>(); // op.AddShardingTableRoute<MyUserRoute>();
}) // })
.UseConfig((sp,o) => // .UseConfig((sp,o) =>
{ // {
o.ThrowIfQueryRouteNotMatch = false; // o.ThrowIfQueryRouteNotMatch = false;
o.UseShardingQuery((conStr, builder) => // o.UseShardingQuery((conStr, builder) =>
{ // {
builder.UseMySql(conStr, new MySqlServerVersion(new Version())) // builder.UseMySql(conStr, new MySqlServerVersion(new Version()))
.UseLoggerFactory(efLogger) // .UseLoggerFactory(efLogger)
.EnableSensitiveDataLogging() // .EnableSensitiveDataLogging()
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); // .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}); // });
o.UseShardingTransaction((connection, builder) => // o.UseShardingTransaction((connection, builder) =>
{ // {
builder // builder
.UseMySql(connection, new MySqlServerVersion(new Version())) // .UseMySql(connection, new MySqlServerVersion(new Version()))
.UseLoggerFactory(efLogger) // .UseLoggerFactory(efLogger)
.EnableSensitiveDataLogging() // .EnableSensitiveDataLogging()
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); // .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}); // });
o.UseShardingMigrationConfigure(b => // o.UseShardingMigrationConfigure(b =>
{ // {
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>(); // b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
}); // });
o.AddDefaultDataSource("ds0", // o.AddDefaultDataSource("ds0",
"server=127.0.0.1;port=3306;database=dbdbdx;userid=root;password=root;"); // "server=127.0.0.1;port=3306;database=dbdbdx;userid=root;password=root;");
}).ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>().AddShardingCore(); // }).ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>().AddShardingCore();
services.AddSingleton<IShardingRuntimeContext>(sp => services.AddShardingDbContext<DefaultShardingDbContext>()
{
Stopwatch stopwatch = Stopwatch.StartNew();
var shardingRuntimeContext = new ShardingRuntimeBuilder<DefaultShardingDbContext>()
.UseRouteConfig(o => .UseRouteConfig(o =>
{ {
o.AddShardingTableRoute<SysUserLogByMonthRoute>(); o.AddShardingTableRoute<SysUserLogByMonthRoute>();
@ -135,14 +131,10 @@ namespace Sample.MySql
{ {
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>(); b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
}); });
}).ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>(ServiceLifetime.Singleton) }).ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>()
.Build(sp); .AddShardingCore();
stopwatch.Stop(); // services.AddDbContext<DefaultShardingDbContext>(ShardingCoreExtension
Console.WriteLine("ShardingRuntimeContext build:" + stopwatch.ElapsedMilliseconds); // .UseMutliDefaultSharding<DefaultShardingDbContext>);
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();
// app.ApplicationServices.UseAutoTryCompensateTable();
var shardingRuntimeContextManager = app.ApplicationServices.GetRequiredService<IShardingRuntimeContextManager>();
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>();