From a2008bce8802184f01bdce21af495a57e07c0c7e Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Mon, 22 Jul 2024 11:22:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A=E5=92=8C?= =?UTF-8?q?=E9=83=A8=E5=88=86=E5=BC=82=E5=B8=B8=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/WeatherForecastController.cs | 289 ++++++++++-------- .../Core/QueryTrackers/IQueryTracker.cs | 6 + .../IShardingRuntimeContext.cs | 7 + .../Internals/InMemoryShardingComparer.cs | 4 + .../AppendOrderSequenceShardingEnumerable.cs | 4 + 5 files changed, 190 insertions(+), 120 deletions(-) diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 9977f98d..196493a9 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -1,4 +1,5 @@ -using System.Diagnostics; +using System; +using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; @@ -50,18 +51,18 @@ namespace Sample.MySql.Controllers public virtual IQueryable Select => this.GetAll(); } - + /// /// 接口限速 /// public class RateLimitAttribute : Attribute { - public RateLimitAttribute(int limitCount = 1) { Console.WriteLine("------------RateLimitAttribute----------"); } } + [ApiController] [Route("[controller]/[action]")] public class WeatherForecastController : ControllerBase @@ -73,37 +74,59 @@ namespace Sample.MySql.Controllers private readonly IShardingRuntimeContext _shardingRuntimeContext; private readonly ABC _abc; - public WeatherForecastController(IShardingRuntimeContext defaultShardingRuntimeContext,IServiceProvider serviceProvider,UnShardingDbContext unShardingDbContext,DefaultShardingDbContext defaultTableDbContext,IShardingRuntimeContext shardingRuntimeContext) + public WeatherForecastController( + IShardingRuntimeContext defaultShardingRuntimeContext, + IServiceProvider serviceProvider, UnShardingDbContext unShardingDbContext, + DefaultShardingDbContext defaultTableDbContext, IShardingRuntimeContext shardingRuntimeContext) { _defaultShardingRuntimeContext = defaultShardingRuntimeContext; _serviceProvider = serviceProvider; _unShardingDbContext = unShardingDbContext; _defaultTableDbContext = defaultTableDbContext; _shardingRuntimeContext = shardingRuntimeContext; - _abc=new ABC(_defaultTableDbContext); + _abc = new ABC(_defaultTableDbContext); } + public (string id,string name___11) aaa() + { + var sql= from u in _defaultTableDbContext.Set() + where u.Id == "123" + select (id:u.Id,name___11:u.UserId); + return sql.FirstOrDefault(); + } public IQueryable GetAll() { + var valueTuple = aaa(); + // valueTuple. + + + var sql = from u in _defaultTableDbContext.Set() + where u.Id == "123" + select new + { + Id=u.Id, + Name_____1111=u.UserId + }; + var sysTests = sql.Where(o=>o.Name_____1111=="456").ToList(); + + var dbContextOptionsBuilder = new DbContextOptionsBuilder(); var dbContextOptionBuilderCreator = _shardingRuntimeContext.GetDbContextOptionBuilderCreator(); dbContextOptionsBuilder.UseDefaultSharding(_shardingRuntimeContext); using (var dbContext = new DefaultShardingDbContext(dbContextOptionsBuilder.Options)) { - } - foreach (var dataSourceDbContext in _defaultTableDbContext.GetShardingExecutor().GetCurrentDbContexts()) { foreach (var keyValuePair in dataSourceDbContext.Value.GetCurrentContexts()) { - } } + ((IResettableService)_defaultTableDbContext).ResetState(); - + var dataSourceRouteManager = _shardingRuntimeContext.GetDataSourceRouteManager(); // dataSourceRouteManager.GetRoute() var routeManager = _shardingRuntimeContext.GetTableRouteManager(); @@ -112,7 +135,7 @@ namespace Sample.MySql.Controllers // DynamicShardingHelper.DynamicAppendDataSource(_shardingRuntimeContext,"ds9","链接字符串",true,true); //如果你已经添加好了的情况下并且没有生成对应的库和表想要生成表和库 var dataSourceInitializer = _shardingRuntimeContext.GetDataSourceInitializer(); - dataSourceInitializer.InitConfigure("ds9",true,true); + dataSourceInitializer.InitConfigure("ds9", true, true); // _defaultTableDbContext.ReadWriteSeparationReadOnly();//读库 // _defaultTableDbContext.ReadWriteSeparationWriteOnly();//写库 @@ -121,23 +144,23 @@ namespace Sample.MySql.Controllers //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可 var tableRoute = tableRouteManager.GetRoute(typeof(SysUserMod)); var virtualTableRoute = (ITailAppendable)tableRoute; - + //一定要先在路由里面添加尾巴 virtualTableRoute.Append("20220921"); - shardingTableCreator.CreateTable("ds0","20220921"); + shardingTableCreator.CreateTable("ds0", "20220921"); return _defaultTableDbContext.Set(); } [HttpGet] public async Task Getxx() { - var test = new Test(); - test.UtcTime=DateTime.Now; - await _defaultTableDbContext.AddAsync(test); + test.UtcTime = DateTime.Now; + await _defaultTableDbContext.AddAsync(test); var saveChangesAsync = await _defaultTableDbContext.SaveChangesAsync(); return Ok(saveChangesAsync); } + [HttpGet] public async Task Get() { @@ -154,13 +177,13 @@ namespace Sample.MySql.Controllers // { // // } - - var dateTime = new DateTime(2021,1,1); + + var dateTime = new DateTime(2021, 1, 1); var x211 = await (from ut in _defaultTableDbContext.Set().UseUnionAllMerge() join uu in _defaultTableDbContext.Set() on ut.Id equals uu.Id - where uu.Time > dateTime - select new { a = ut, b = uu }).Select(o=>new {x=o}).Select(o=>new{x=o}) + where uu.Time > dateTime + select new { a = ut, b = uu }).Select(o => new { x = o }).Select(o => new { x = o }) .Select(o => new { o.x.x.a.Id @@ -169,19 +192,19 @@ namespace Sample.MySql.Controllers var x2 = await (from ut in _defaultTableDbContext.Set() join uu in _defaultTableDbContext.Set() on ut.Id equals uu.Id - select new { a = ut, b = uu }).Select(o=>new {x=o}).Select(o=>new{x=o}) + select new { a = ut, b = uu }).Select(o => new { x = o }).Select(o => new { x = o }) .Select(o => new { o.x.x.a.Id }).OrderBy(o => o.Id).ToListAsync(); Console.WriteLine("123"); - //OtherDbContext.CurrentId = ""; + //OtherDbContext.CurrentId = ""; // var myUsers0 = _otherDbContext.MyUsers.ToList(); // OtherDbContext.CurrentId = "123"; // var myUsers1 = _otherDbContext.MyUsers.ToList(); // OtherDbContext.CurrentId = "456"; // var myUsers2= _otherDbContext.MyUsers.ToList(); - + // var sysUserModQueryable = _otherDbContext.MyUsers.Where(o => o.Id == "2"); // var dbSetDiscoverExpressionVisitor = new DbSetDiscoverExpressionVisitor(_otherDbContext); // dbSetDiscoverExpressionVisitor.Visit(sysUserModQueryable.Expression); @@ -189,12 +212,12 @@ namespace Sample.MySql.Controllers // Console.WriteLine("------------"); // using (var tran = _defaultTableDbContext.Database.BeginTransaction()) // { - + // SysUserMod? resultX123 = await _defaultTableDbContext.Set() // .Where(o => o.Id == "2").FirstOrDefaultAsync(); // _defaultTableDbContext.Update(resultX123); // _defaultTableDbContext.SaveChanges(); - + Stopwatch sp = Stopwatch.StartNew(); var sysUserMods = await _defaultTableDbContext.Set().ToListAsync(); sp.Stop(); @@ -207,17 +230,17 @@ namespace Sample.MySql.Controllers var sysUserMods22 = await _defaultTableDbContext.Set().ToListAsync(); sp.Stop(); Console.WriteLine(sp.ElapsedMilliseconds); - + var resultX1 = await _defaultTableDbContext.Set() - .Where(o => o.Id == "2" || o.Id == "3").GroupBy(o => new { o.Id,o.Name }) - .Select(o => new - { - id = o.Key.Id, - name = o.Key.Name, - count = o.Count() - }).ToListAsync(); + .Where(o => o.Id == "2" || o.Id == "3").GroupBy(o => new { o.Id, o.Name }) + .Select(o => new + { + id = o.Key.Id, + name = o.Key.Name, + count = o.Count() + }).ToListAsync(); var resultX12 = await _defaultTableDbContext.Set() - .Where(o => o.Id == "2" || o.Id == "3").GroupBy(o => new { o.Id,o.Name }) + .Where(o => o.Id == "2" || o.Id == "3").GroupBy(o => new { o.Id, o.Name }) .Select(o => new abc { id = o.Key.Id, @@ -232,132 +255,147 @@ namespace Sample.MySql.Controllers Console.WriteLine("1"); } - var x=await (from ut in _defaultTableDbContext.Set() - from uu in _defaultTableDbContext.Set() - where ut.Id == uu.Id + var x = await (from ut in _defaultTableDbContext.Set() + from uu in _defaultTableDbContext.Set() + where ut.Id == uu.Id select ut).FirstOrDefaultAsync(); - var x1=await (from ut in _defaultTableDbContext.Set() + var x1 = await (from ut in _defaultTableDbContext.Set() from uu in _defaultTableDbContext.Set() - where ut.Id == uu.Id + where ut.Id == uu.Id select ut).FirstOrDefaultAsync(); // var firstOrDefault = _defaultTableDbContext.Set().FromSqlRaw($"select * from {nameof(SysUserMod)}").FirstOrDefault(); var sysUserMods1 = _defaultTableDbContext.Set().UseConnectionMode(1) - .Select(o => new ssss(){ Id = o.Id, C = _abc.Select.Count(x => x.Id == o.Id) }).ToList(); + .Select(o => new ssss() { Id = o.Id, C = _abc.Select.Count(x => x.Id == o.Id) }).ToList(); var sysUserMods2 = _defaultTableDbContext.Set() - .Select(o => new ssss(){ Id = o.Id, C = GetAll().Count(x => x.Id == o.Id) }).ToList(); + .Select(o => new ssss() { Id = o.Id, C = GetAll().Count(x => x.Id == o.Id) }).ToList(); var sysTests = GetAll(); var sysUserMods3 = _defaultTableDbContext.Set() // .AsRoute(op=> // { // op.TryCreateOrAddMustTail(new[] { "00" }); // }) - .Select(o => new ssss(){ Id = o.Id, C = sysTests.Count(x => x.Id == o.Id) }).ToList(); + .Select(o => new ssss() { Id = o.Id, C = sysTests.Count(x => x.Id == o.Id) }).ToList(); var resultX = await _defaultTableDbContext.Set() - .Where(o => o.Id == "2" || o.Id == "3").FirstOrDefaultAsync(); - var resultY = await _defaultTableDbContext.Set().FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3"); - var shardingFirstOrDefaultAsyncxxx = await _defaultTableDbContext.Set().Where(o=>o.Time==DateTime.Now).ToListAsync(); - var result = await _defaultTableDbContext.Set().AnyAsync(); - var result22 = await _defaultTableDbContext.Set().Where(o => o.Id == "2"&&o.Name=="ds1").ToListAsync(); - var result1 = await _defaultTableDbContext.Set().Where(o => o.Id == "2" || o.Id == "3").ToListAsync(); - var result2 = await _defaultTableDbContext.Set().Skip(1).Take(10).ToListAsync(); - var shardingFirstOrDefaultAsync = await _defaultTableDbContext.Set().ToListAsync(); - var shardingCountAsync = await _defaultTableDbContext.Set().CountAsync(); - var shardingCountAsyn2c = _defaultTableDbContext.Set().Count(); - - - // var dbConnection = _defaultTableDbContext.Database.GetDbConnection(); - // if (dbConnection.State != ConnectionState.Open) - // { - // dbConnection.Open(); - // } - // using (var dbCommand = dbConnection.CreateCommand()) - // { - // dbCommand.CommandText = "select * from systest"; - // dbCommand.Transaction = _defaultTableDbContext.Database.CurrentTransaction?.GetDbTransaction(); - // var dbDataReader = dbCommand.ExecuteReader(); - // while (dbDataReader.Read()) - // { - // Console.WriteLine(dbDataReader[0]); - // } - // } + .Where(o => o.Id == "2" || o.Id == "3").FirstOrDefaultAsync(); + var resultY = await _defaultTableDbContext.Set() + .FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3"); + var shardingFirstOrDefaultAsyncxxx = await _defaultTableDbContext.Set() + .Where(o => o.Time == DateTime.Now).ToListAsync(); + var result = await _defaultTableDbContext.Set().AnyAsync(); + var result22 = await _defaultTableDbContext.Set().Where(o => o.Id == "2" && o.Name == "ds1") + .ToListAsync(); + var result1 = await _defaultTableDbContext.Set().Where(o => o.Id == "2" || o.Id == "3") + .ToListAsync(); + var result2 = await _defaultTableDbContext.Set().Skip(1).Take(10).ToListAsync(); + var shardingFirstOrDefaultAsync = await _defaultTableDbContext.Set().ToListAsync(); + var shardingCountAsync = await _defaultTableDbContext.Set().CountAsync(); + var shardingCountAsyn2c = _defaultTableDbContext.Set().Count(); + + + // var dbConnection = _defaultTableDbContext.Database.GetDbConnection(); + // if (dbConnection.State != ConnectionState.Open) + // { + // dbConnection.Open(); // } - + // using (var dbCommand = dbConnection.CreateCommand()) + // { + // dbCommand.CommandText = "select * from systest"; + // dbCommand.Transaction = _defaultTableDbContext.Database.CurrentTransaction?.GetDbTransaction(); + // var dbDataReader = dbCommand.ExecuteReader(); + // while (dbDataReader.Read()) + // { + // Console.WriteLine(dbDataReader[0]); + // } + // } + // } + return Ok(1); } + [HttpGet] public async Task Get1() { var resultX = await _defaultTableDbContext.Set() .Where(o => o.Id == "2" || o.Id == "3").FirstOrDefaultAsync(); - Console.WriteLine("-----------------------------------------------------------------------------------------------------"); - var resultY = await _defaultTableDbContext.Set().FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3"); - Console.WriteLine("-----------------------------------------------------------------------------------------------------"); + Console.WriteLine( + "-----------------------------------------------------------------------------------------------------"); + var resultY = await _defaultTableDbContext.Set() + .FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3"); + Console.WriteLine( + "-----------------------------------------------------------------------------------------------------"); var result = await _defaultTableDbContext.Set().AnyAsync(); - Console.WriteLine("-----------------------------------------------------------------------------------------------------"); + Console.WriteLine( + "-----------------------------------------------------------------------------------------------------"); return Ok(); } + [HttpGet] public async Task Get2() { - var dateTime = new DateTime(2021,1,1); - var sql= from a in _defaultTableDbContext.Set().Where(o=>o.Time==dateTime) + var dateTime = new DateTime(2021, 1, 1); + var sql = from a in _defaultTableDbContext.Set().Where(o => o.Time == dateTime) join b in _defaultTableDbContext.Set() - on a.Id equals b.Id into t1 + on a.Id equals b.Id into t1 from aa1 in t1.DefaultIfEmpty() select new { ID = a.Id }; - var listAsync =await sql.ToListAsync(); - // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); - // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); + var listAsync = await sql.ToListAsync(); + // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); + // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(); } + [HttpGet] public async Task Get3() { var sysUserMods = await _defaultTableDbContext.Set().FirstOrDefaultAsync(); _defaultTableDbContext.SysUserMod.Remove(sysUserMods); _defaultTableDbContext.SaveChanges(); - // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); - // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); + // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); + // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(); } + [HttpGet] public async Task Get4() { - var sql=from a in _defaultTableDbContext.Set() + var sql = from a in _defaultTableDbContext.Set() join b in _defaultTableDbContext.Set() on a.Id equals b.MainId select a; var xx = await sql.ToListAsync(); // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); - // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); + // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(); } + [HttpGet] public async Task Get6() { var sysUserMod = await _defaultTableDbContext.Set().FirstOrDefaultAsync(); - sysUserMod.Age = new Random().Next(1,999); + sysUserMod.Age = new Random().Next(1, 999); _defaultTableDbContext.Update(sysUserMod); _defaultTableDbContext.SaveChanges(); // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(); } + [HttpGet] public async Task Get7() { var sysUserMod = await _defaultTableDbContext.Set().FindAsync("101"); - sysUserMod.Age = new Random().Next(1,999); + sysUserMod.Age = new Random().Next(1, 999); _defaultTableDbContext.Update(sysUserMod); _defaultTableDbContext.SaveChanges(); // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(); } + [HttpGet] public async Task Get8() { @@ -368,15 +406,18 @@ namespace Sample.MySql.Controllers // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(); } + [HttpGet] public async Task Get9() { - var sysUserMod = await _defaultTableDbContext.Set().Where(o=>o.Name=="11231").AllAsync(o=>o.Id=="1123"); - + var sysUserMod = await _defaultTableDbContext.Set().Where(o => o.Name == "11231") + .AllAsync(o => o.Id == "1123"); + // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(); } + [HttpGet] public async Task Get10() { @@ -384,23 +425,26 @@ namespace Sample.MySql.Controllers var dataSourceDbContexts = shardingDbContextExecutor.GetCurrentDbContexts(); var sourceDbContexts = dataSourceDbContexts.Values; var dataSourceDbContext = sourceDbContexts.FirstOrDefault(); - var dbConnection = dataSourceDbContext.GetCurrentContexts().FirstOrDefault().Value.Database.GetDbConnection(); + var dbConnection = dataSourceDbContext.GetCurrentContexts().FirstOrDefault().Value.Database + .GetDbConnection(); - var sysUserMod1 = await _defaultTableDbContext.Set().Where(o=>o.UserId=="11231").AllAsync(o=>o.Id=="1123"&&string.Compare(o.UserId,"123")>0); - var sysUserMod2 = await _defaultTableDbContext.Set().AllAsync(o=>o.Id=="1123"); - var dateTime = new DateTime(2020,1,1); + var sysUserMod1 = await _defaultTableDbContext.Set().Where(o => o.UserId == "11231") + .AllAsync(o => o.Id == "1123" && string.Compare(o.UserId, "123") > 0); + var sysUserMod2 = await _defaultTableDbContext.Set().AllAsync(o => o.Id == "1123"); + var dateTime = new DateTime(2020, 1, 1); var sysUserMod3 = await _defaultTableDbContext.Set().Where(o => o.Time > dateTime) .ToListAsync(); - + // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(); } + [HttpGet] public async Task Get11() { - using(var tran = await _defaultTableDbContext.Database.BeginTransactionAsync()) + using (var tran = await _defaultTableDbContext.Database.BeginTransactionAsync()) { var newGuid = Guid.NewGuid().ToString("n"); await _defaultTableDbContext.Set().Where(o => o.Id == "11") @@ -411,21 +455,23 @@ namespace Sample.MySql.Controllers return Ok(); } + [HttpGet] public async Task Get12() { var sysTests = from ut in _defaultTableDbContext.Set() where _defaultTableDbContext.Set().Any(x => x.Id == ut.Id) - select ut; + select ut; var tests = sysTests.ToList(); return Ok(); } + [HttpGet] public async Task Get13() { - var list =await _defaultTableDbContext.Set() - .GroupBy(o=>o.Time) - .Select(o=>new + var list = await _defaultTableDbContext.Set() + .GroupBy(o => o.Time) + .Select(o => new { o.Key }).ToListAsync(); @@ -441,7 +487,7 @@ namespace Sample.MySql.Controllers { var sysUserLogByMonth = new SysUserLogByMonth(); sysUserLogByMonth.Id = Guid.NewGuid().ToString("n"); - sysUserLogByMonth.Time=DateTime.Now; + sysUserLogByMonth.Time = DateTime.Now; sysUserLogByMonths.Add(sysUserLogByMonth); } @@ -459,7 +505,7 @@ namespace Sample.MySql.Controllers { var sysUserLogByMonth = new SysUserLogByMonth(); sysUserLogByMonth.Id = Guid.NewGuid().ToString("n"); - sysUserLogByMonth.Time=DateTime.Now; + sysUserLogByMonth.Time = DateTime.Now; sysUserLogByMonths.Add(sysUserLogByMonth); } @@ -467,16 +513,15 @@ namespace Sample.MySql.Controllers await _unShardingDbContext.SaveChangesAsync(); return Ok(); } - - - + + [HttpGet] public async Task Get16() { _defaultTableDbContext.ReadWriteSeparationWriteOnly(); - + var sysUserMod = await _defaultTableDbContext.Set().FirstOrDefaultAsync(); - sysUserMod.Age = new Random().Next(1,999); + sysUserMod.Age = new Random().Next(1, 999); await _defaultTableDbContext.SaveChangesAsync(); // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); @@ -497,32 +542,36 @@ namespace Sample.MySql.Controllers // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(sysUserMods); } + [HttpGet] public async Task Get18() { var sysUserMods = await _defaultTableDbContext.SysUserMod.AsNoTracking().GroupBy(o => o.Age) - .Select(o=>new + .Select(o => new { - Age=o.Key + Age = o.Key }) .CountAsync(); - + // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(sysUserMods); } + [HttpGet] public async Task Get20() { - var dateTime = new DateTime(2022,1,1); - var queryable = _defaultTableDbContext.Set().Where(o=>o.Time>dateTime).Select(o=>o.Id); + var dateTime = new DateTime(2022, 1, 1); + var queryable = _defaultTableDbContext.Set().Where(o => o.Time > dateTime) + .Select(o => o.Id); var sysUserMods = await _defaultTableDbContext.SysUserMod.AsNoTracking() .Where(o => queryable.Contains(o.Id)).ToListAsync(); - + // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(sysUserMods); } + [HttpGet] public async Task Get211() { @@ -556,13 +605,13 @@ namespace Sample.MySql.Controllers public void get11() { var mySqlConnection = new MySqlConnection("链接字符串"); - var dbContextOptions1 = new DbContextOptionsBuilder().UseMySql(mySqlConnection,new MySqlServerVersion(new Version()),b=> - { - b.EnableRetryOnFailure(10, TimeSpan.FromSeconds(10), null); - }).Options; - var dbContextOptions2 = new DbContextOptionsBuilder().UseMySql(mySqlConnection,new MySqlServerVersion(new Version())).Options; - var unShardingDbContext1 = new UnShardingDbContext(dbContextOptions1);//映射到202301模型 - var unShardingDbContext2 = new UnShardingDbContext(dbContextOptions2);//映射到202302模型 + var dbContextOptions1 = new DbContextOptionsBuilder().UseMySql(mySqlConnection, + new MySqlServerVersion(new Version()), + b => { b.EnableRetryOnFailure(10, TimeSpan.FromSeconds(10), null); }).Options; + var dbContextOptions2 = new DbContextOptionsBuilder() + .UseMySql(mySqlConnection, new MySqlServerVersion(new Version())).Options; + var unShardingDbContext1 = new UnShardingDbContext(dbContextOptions1); //映射到202301模型 + var unShardingDbContext2 = new UnShardingDbContext(dbContextOptions2); //映射到202302模型 // unShardingDbContext2.Database.CreateExecutionStrategy() var dbContextTransaction = unShardingDbContext1.Database.BeginTransaction(); unShardingDbContext2.Database.UseTransaction(dbContextTransaction.GetDbTransaction()); @@ -577,10 +626,10 @@ namespace Sample.MySql.Controllers public async Task get131() { var list = new List(); - var idList = Enumerable.Range(1,50000).Select(o=>o.ToString()).ToList(); + var idList = Enumerable.Range(1, 50000).Select(o => o.ToString()).ToList(); var sysUserMods = _defaultTableDbContext.Set() - .Where(o=>idList.Contains(o.Id)).ToList(); + .Where(o => idList.Contains(o.Id)).ToList(); return Ok(); } } -} +} \ No newline at end of file diff --git a/src/ShardingCore/Core/QueryTrackers/IQueryTracker.cs b/src/ShardingCore/Core/QueryTrackers/IQueryTracker.cs index 05e3f590..ac9ec705 100644 --- a/src/ShardingCore/Core/QueryTrackers/IQueryTracker.cs +++ b/src/ShardingCore/Core/QueryTrackers/IQueryTracker.cs @@ -9,6 +9,12 @@ namespace ShardingCore.Core.QueryTrackers { public interface IQueryTracker { + /// + /// 追踪数据对象 + /// + /// + /// + /// public object Track(object entity,IShardingDbContext shardingDbContext); } } diff --git a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs index fbb3a8d7..d4996349 100644 --- a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs +++ b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs @@ -30,7 +30,14 @@ namespace ShardingCore.Core.RuntimeContexts public interface IShardingRuntimeContext { + /// + /// 上下文类型 + /// Type DbContextType { get; } + /// + /// 模型锁提供者 + /// + /// IModelCacheLockerProvider GetModelCacheLockerProvider(); IDbContextTypeAware GetDbContextTypeAware(); IShardingProvider GetShardingProvider(); diff --git a/src/ShardingCore/Sharding/Internals/InMemoryShardingComparer.cs b/src/ShardingCore/Sharding/Internals/InMemoryShardingComparer.cs index 83a0e618..72b19f68 100644 --- a/src/ShardingCore/Sharding/Internals/InMemoryShardingComparer.cs +++ b/src/ShardingCore/Sharding/Internals/InMemoryShardingComparer.cs @@ -6,6 +6,10 @@ using ShardingCore.Sharding.ShardingComparision.Abstractions; namespace ShardingCore.Sharding.Internals { + /// + /// 内存分片比较器 + /// + /// public class InMemoryShardingComparer : IComparer { private readonly IShardingComparer _shardingComparer; diff --git a/src/ShardingCore/Sharding/MergeEngines/Enumerables/AppendOrderSequenceShardingEnumerable.cs b/src/ShardingCore/Sharding/MergeEngines/Enumerables/AppendOrderSequenceShardingEnumerable.cs index 22670c1e..7be13640 100644 --- a/src/ShardingCore/Sharding/MergeEngines/Enumerables/AppendOrderSequenceShardingEnumerable.cs +++ b/src/ShardingCore/Sharding/MergeEngines/Enumerables/AppendOrderSequenceShardingEnumerable.cs @@ -74,6 +74,10 @@ namespace ShardingCore.Sharding.MergeEngines.Enumerables } else { + if (_tableSequenceOrderConfig == null) + { + throw new ShardingCoreException("_tableSequenceOrderConfig is null,plz config IPaginationConfiguration."); + } var appendAsc = _tableSequenceOrderConfig.AppendAsc; if (appendAsc)