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,