From ede5c3428637fad73d410ac404d871df0376fa49 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Fri, 1 Apr 2022 10:01:39 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=B8=8D=E8=83=BD=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ValuesController.cs | 12 +++++++++ samples/Sample.SqlServer/Startup.cs | 3 +-- .../AbstractShardingAbpDbContext.cs | 2 ++ .../EFCores/EFCore6x/ShardingModelSource.cs | 2 +- .../EFCores/ShardingChangeTracker.cs | 26 +++++++++---------- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/samples/Sample.SqlServer/Controllers/ValuesController.cs b/samples/Sample.SqlServer/Controllers/ValuesController.cs index 11204a34..18e0c3ef 100644 --- a/samples/Sample.SqlServer/Controllers/ValuesController.cs +++ b/samples/Sample.SqlServer/Controllers/ValuesController.cs @@ -17,6 +17,9 @@ using ShardingCore.Core.VirtualDatabase.VirtualTables; using ShardingCore.Core.VirtualRoutes.TableRoutes; using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine; using ShardingCore.Extensions.ShardingQueryableExtensions; +using ShardingCore.Sharding.Abstractions; +using Microsoft.EntityFrameworkCore.Query.Internal; +using ShardingCore.EFCores; namespace Sample.SqlServer.Controllers { @@ -88,6 +91,7 @@ namespace Sample.SqlServer.Controllers // }; //var listAsync = await sql.ToListAsync(); //var resultx112331tt = await _defaultTableDbContext.Set().AsNoTracking().CountAsync(); + var resultx112331tt2 = await _defaultTableDbContext.Set().FirstOrDefaultAsync(o => o.Id == "2"); var resultx112331ttaa2 = await _defaultTableDbContext.Set().FirstOrDefaultAsync(o => o.Id == "2"); resultx112331ttaa2.UserId = "zzzz"; @@ -151,6 +155,8 @@ namespace Sample.SqlServer.Controllers var hashSet = unionUserIds.Select(o => o.UserId).ToHashSet(); var hashSetCount = hashSet.Count; + + return Ok(); } @@ -162,6 +168,12 @@ namespace Sample.SqlServer.Controllers public async Task Get1([FromQuery] int p, [FromQuery] int s) { + //var queryable= _defaultTableDbContext.Set().AsNoSequence().Where(o => true); + // var provider = queryable.Provider as EntityQueryProvider; + // var compiler = provider.GetFieldValue("_queryCompiler") as ShardingQueryCompiler; + // var shardingDbContext = compiler.GetFieldValue("_shardingDbContext") as IShardingDbContext; + + Stopwatch sp = new Stopwatch(); sp.Start(); var shardingPageResultAsync = await _defaultTableDbContext.Set() diff --git a/samples/Sample.SqlServer/Startup.cs b/samples/Sample.SqlServer/Startup.cs index 192e9ed9..42c67cbd 100644 --- a/samples/Sample.SqlServer/Startup.cs +++ b/samples/Sample.SqlServer/Startup.cs @@ -49,8 +49,7 @@ namespace Sample.SqlServer }); op.ReplaceTableEnsureManager(sp => new SqlServerTableEnsureManager()); op.AddDefaultDataSource("A", - // "Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;" - "Data Source = 101.37.117.55;persist security info=True;Initial Catalog=ShardingCoreDBXA;uid=sa;pwd=xjmumixl7610#;Max Pool Size=100;" + "Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;" ); }).EnsureConfig(); //services.AddShardingDbContext( diff --git a/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs b/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs index bdb5c0da..610df8cc 100644 --- a/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs +++ b/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs @@ -20,6 +20,7 @@ using ShardingCore.Core.VirtualDatabase.VirtualDataSources; namespace Samples.AbpSharding { + public abstract class AbstractShardingAbpDbContext : AbpDbContext, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite where TDbContext : DbContext { @@ -570,5 +571,6 @@ namespace Samples.AbpSharding { return _shardingDbContextExecutor.GetVirtualDataSource(); } + } } \ No newline at end of file diff --git a/src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs b/src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs index 848810d7..29e0fbed 100644 --- a/src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs +++ b/src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs @@ -27,7 +27,7 @@ namespace ShardingCore.EFCores public ShardingModelSource(ModelSourceDependencies dependencies) : base(dependencies) { Check.NotNull(dependencies, nameof(dependencies)); - + Console.WriteLine($"ShardingModelSource:{Guid.NewGuid()}"); Dependencies = dependencies; } diff --git a/src/ShardingCore/EFCores/ShardingChangeTracker.cs b/src/ShardingCore/EFCores/ShardingChangeTracker.cs index 6df9f4aa..24de238d 100644 --- a/src/ShardingCore/EFCores/ShardingChangeTracker.cs +++ b/src/ShardingCore/EFCores/ShardingChangeTracker.cs @@ -12,19 +12,19 @@ using ShardingCore.Sharding.Abstractions; namespace ShardingCore.EFCores { - public class ShardingChangeTracker: ChangeTracker - { - private readonly ICurrentDbContextDiscover _contextDiscover; + //public class ShardingChangeTracker: ChangeTracker + //{ + // private readonly ICurrentDbContextDiscover _contextDiscover; - public ShardingChangeTracker(DbContext context, IStateManager stateManager, IChangeDetector changeDetector, IModel model, IEntityEntryGraphIterator graphIterator) : base(context, stateManager, changeDetector, model, graphIterator) - { - _contextDiscover = context as ICurrentDbContextDiscover?? throw new ShardingCoreNotSupportException($"{context.GetType()} not impl {nameof(ICurrentDbContextDiscover)}"); - } + // public ShardingChangeTracker(DbContext context, IStateManager stateManager, IChangeDetector changeDetector, IModel model, IEntityEntryGraphIterator graphIterator) : base(context, stateManager, changeDetector, model, graphIterator) + // { + // _contextDiscover = context as ICurrentDbContextDiscover?? throw new ShardingCoreNotSupportException($"{context.GetType()} not impl {nameof(ICurrentDbContextDiscover)}"); + // } - public override bool HasChanges() - { - return _contextDiscover.GetCurrentDbContexts().Any(o => - o.Value.GetCurrentContexts().Any(r => r.Value.ChangeTracker.HasChanges())); - } - } + // public override bool HasChanges() + // { + // return _contextDiscover.GetCurrentDbContexts().Any(o => + // o.Value.GetCurrentContexts().Any(r => r.Value.ChangeTracker.HasChanges())); + // } + //} }