From a33de9395a6d5488873911a65fae2418e0d78eb9 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Thu, 22 Sep 2022 08:46:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20DynamicShardingHelper=20?= =?UTF-8?q?=E7=9A=84=E5=8A=A8=E6=80=81=E6=B7=BB=E5=8A=A0=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/WeatherForecastController.cs | 3 ++- src/ShardingCore/Helpers/DynamicShardingHelper.cs | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 59005d92..5d437904 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -6,6 +6,7 @@ using Sample.MySql.multi; using Sample.MySql.Shardings; using ShardingCore.Core.RuntimeContexts; using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions; +using ShardingCore.Helpers; namespace Sample.MySql.Controllers { @@ -57,7 +58,7 @@ namespace Sample.MySql.Controllers // Console.WriteLine("------------"); // using (var tran = _defaultTableDbContext.Database.BeginTransaction()) // { - var sysUserMods = _defaultTableDbContext.Set().OrderBy(o=>o.Id).ThenBy(o=>o.Name); + var firstOrDefault = _defaultTableDbContext.Set().FromSqlRaw($"select * from {nameof(SysUserMod)}").FirstOrDefault(); var sysUserMods1 = _defaultTableDbContext.Set() .Select(o => new ssss(){ Id = o.Id, C = GetAll().Count(x => x.Id == o.Id) }).ToList(); diff --git a/src/ShardingCore/Helpers/DynamicShardingHelper.cs b/src/ShardingCore/Helpers/DynamicShardingHelper.cs index c803b808..ccb9ecb5 100644 --- a/src/ShardingCore/Helpers/DynamicShardingHelper.cs +++ b/src/ShardingCore/Helpers/DynamicShardingHelper.cs @@ -7,7 +7,6 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using ShardingCore.Core.DbContextCreator; using ShardingCore.Core.RuntimeContexts; -using ShardingCore.Core.ShardingMigrations.Abstractions; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.EFCores; using ShardingCore.Exceptions; @@ -36,8 +35,11 @@ namespace ShardingCore.Helpers { var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); virtualDataSource.AddPhysicDataSource(new DefaultPhysicDataSource(dataSourceName, connectionString, false)); - var dataSourceInitializer = shardingRuntimeContext.GetDataSourceInitializer(); - dataSourceInitializer.InitConfigure(dataSourceName,createDatabase,createTable); + if (createDatabase || createTable) + { + var dataSourceInitializer = shardingRuntimeContext.GetDataSourceInitializer(); + dataSourceInitializer.InitConfigure(dataSourceName,createDatabase,createTable); + } } /// /// 动态添加数据源 @@ -47,8 +49,7 @@ namespace ShardingCore.Helpers /// public static void DynamicAppendDataSourceOnly(IShardingRuntimeContext shardingRuntimeContext, string dataSourceName, string connectionString) { - var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - virtualDataSource.AddPhysicDataSource(new DefaultPhysicDataSource(dataSourceName, connectionString, false)); + DynamicAppendDataSource(shardingRuntimeContext, dataSourceName, connectionString, false, false); } public static async Task DynamicMigrateWithDataSourcesAsync(IShardingRuntimeContext shardingRuntimeContext,