diff --git a/nuget-publish.bat b/nuget-publish.bat index 8c9fa25f..8422d68a 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,9 +1,9 @@ :start ::定义版本 -set EFCORE2=2.4.1.01-rc1 -set EFCORE3=3.4.1.01-rc1 -set EFCORE5=5.4.1.01-rc1 -set EFCORE6=6.4.1.01-rc1 +set EFCORE2=2.4.1.01 +set EFCORE3=3.4.1.01 +set EFCORE5=5.4.1.01 +set EFCORE6=6.4.1.01 ::删除所有bin与obj下的文件 @echo off diff --git a/samples/Sample.Migrations/DefaultDesignTimeDbContextFactory.cs b/samples/Sample.Migrations/DefaultDesignTimeDbContextFactory.cs index 58b8d18c..83c9916c 100644 --- a/samples/Sample.Migrations/DefaultDesignTimeDbContextFactory.cs +++ b/samples/Sample.Migrations/DefaultDesignTimeDbContextFactory.cs @@ -21,8 +21,9 @@ namespace Sample.Migrations services.AddShardingDbContext() .AddEntityConfig(o => { - o.CreateShardingTableOnStart = true; - o.EnsureCreatedWithOutShardingTable = true; + o.CreateShardingTableOnStart = false; + o.CreateDataBaseOnlyOnStart = true; + o.EnsureCreatedWithOutShardingTable = false; o.AddShardingTableRoute(); o.AddShardingTableRoute(); }) diff --git a/samples/Samples.AutoByDate.SqlServer/Domain/Entities/SysUserLogByDay.cs b/samples/Samples.AutoByDate.SqlServer/Domain/Entities/SysUserLogByDay.cs index 11a616b2..c5574f68 100644 --- a/samples/Samples.AutoByDate.SqlServer/Domain/Entities/SysUserLogByDay.cs +++ b/samples/Samples.AutoByDate.SqlServer/Domain/Entities/SysUserLogByDay.cs @@ -9,11 +9,10 @@ namespace Samples.AutoByDate.SqlServer.Domain.Entities * @Date: Tuesday, 02 February 2021 17:00:36 * @Email: 326308290@qq.com */ - public class SysUserLogByDay:IShardingTable + public class SysUserLogByDay { public int Id { get; set; } public string Body { get; set; } - [ShardingTableKey] public DateTime CreateTime { get; set; } } } \ No newline at end of file diff --git a/samples/Samples.AutoByDate.SqlServer/Jobs/AutoCreateTableByDay.cs b/samples/Samples.AutoByDate.SqlServer/Jobs/AutoCreateTableByDay.cs deleted file mode 100644 index f8936421..00000000 --- a/samples/Samples.AutoByDate.SqlServer/Jobs/AutoCreateTableByDay.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System; -using ChronusJob.Abstractions; -using ChronusJob.Jobs.Attributes; -using Samples.AutoByDate.SqlServer.DbContexts; -using Samples.AutoByDate.SqlServer.Domain.Entities; -using ShardingCore; -using ShardingCore.Core.PhysicTables; -using ShardingCore.Core.VirtualDatabase.VirtualTables; -using ShardingCore.Core.VirtualTables; -using ShardingCore.TableCreator; - -namespace Samples.AutoByDate.SqlServer.Jobs -{ - /* - * @Author: xjm - * @Description: - * @Date: Tuesday, 02 February 2021 17:24:17 - * @Email: 326308290@qq.com - */ - public class AutoCreateTableByDay : IJob - { - /// - /// 每天中午12点执行,启动的时候执行以下 - /// - /// - /// - [JobRun(Name = "定时创建分表组件", Cron = "0 0 12 * * ?", RunOnceOnStart = true)] - - public void AutoCreateTable(IVirtualTableManager virtualTableManager, IShardingTableCreator tableCreator) - { - var virtualTable = virtualTableManager.GetVirtualTable(typeof(SysUserLogByDay)); - if (virtualTable == null) - { - return; - } - var now = DateTime.Now.Date.AddDays(1); - var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now); - try - { - virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail)); - tableCreator.CreateTable("ds0", typeof(SysUserLogByDay), tail); - } - catch (Exception e) - { - //ignore - } - } - } -} \ No newline at end of file diff --git a/samples/Samples.AutoByDate.SqlServer/Samples.AutoByDate.SqlServer.csproj b/samples/Samples.AutoByDate.SqlServer/Samples.AutoByDate.SqlServer.csproj index edda3a21..efad5db9 100644 --- a/samples/Samples.AutoByDate.SqlServer/Samples.AutoByDate.SqlServer.csproj +++ b/samples/Samples.AutoByDate.SqlServer/Samples.AutoByDate.SqlServer.csproj @@ -5,7 +5,6 @@ - diff --git a/samples/Samples.AutoByDate.SqlServer/Shardings/SysUserLogByDayVirtualTableRoute.cs b/samples/Samples.AutoByDate.SqlServer/Shardings/SysUserLogByDayVirtualTableRoute.cs index 0fdf3b24..a097b203 100644 --- a/samples/Samples.AutoByDate.SqlServer/Shardings/SysUserLogByDayVirtualTableRoute.cs +++ b/samples/Samples.AutoByDate.SqlServer/Shardings/SysUserLogByDayVirtualTableRoute.cs @@ -28,7 +28,7 @@ namespace Samples.AutoByDate.SqlServer.Shardings public override void Configure(EntityMetadataTableBuilder builder) { - + builder.ShardingProperty(o => o.CreateTime); } } } \ No newline at end of file diff --git a/samples/Samples.AutoByDate.SqlServer/Startup.cs b/samples/Samples.AutoByDate.SqlServer/Startup.cs index 7baeed26..675dc61c 100644 --- a/samples/Samples.AutoByDate.SqlServer/Startup.cs +++ b/samples/Samples.AutoByDate.SqlServer/Startup.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using ChronusJob; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; @@ -50,9 +49,8 @@ namespace Samples.AutoByDate.SqlServer { builder.UseSqlServer(connection); }); - sp.AddDefaultDataSource("ds0", "Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;"); + sp.AddDefaultDataSource("ds0", "Data Source=localhost;Initial Catalog=ShardingCoreDBz;Integrated Security=True;"); }).EnsureConfig(); - services.AddChronusJob(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/src/ShardingCore/Core/ShardingConfigurations/Abstractions/IShardingEntityConfigOptions.cs b/src/ShardingCore/Core/ShardingConfigurations/Abstractions/IShardingEntityConfigOptions.cs index f53c2205..fe995258 100644 --- a/src/ShardingCore/Core/ShardingConfigurations/Abstractions/IShardingEntityConfigOptions.cs +++ b/src/ShardingCore/Core/ShardingConfigurations/Abstractions/IShardingEntityConfigOptions.cs @@ -20,6 +20,10 @@ namespace ShardingCore.Core.ShardingConfigurations.Abstractions /// bool? CreateShardingTableOnStart { get; set; } /// + /// 是否在启动时创建数据库 + /// + public bool? CreateDataBaseOnlyOnStart { get; set; } + /// /// 当查询遇到没有路由被命中时是否抛出错误 /// bool ThrowIfQueryRouteNotMatch { get; set; } diff --git a/src/ShardingCore/Core/ShardingConfigurations/ShardingEntityConfigOptions.cs b/src/ShardingCore/Core/ShardingConfigurations/ShardingEntityConfigOptions.cs index ab4e0da1..57a10cad 100644 --- a/src/ShardingCore/Core/ShardingConfigurations/ShardingEntityConfigOptions.cs +++ b/src/ShardingCore/Core/ShardingConfigurations/ShardingEntityConfigOptions.cs @@ -31,6 +31,10 @@ namespace ShardingCore.Core.ShardingConfigurations /// public bool? CreateShardingTableOnStart { get; set; } /// + /// 是否在启动时创建数据库 + /// + public bool? CreateDataBaseOnlyOnStart { get; set; } + /// /// 当查询遇到没有路由被命中时是否抛出错误 /// public bool ThrowIfQueryRouteNotMatch { get; set; } = true; diff --git a/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs b/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs index e002a33e..10532008 100644 --- a/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs +++ b/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs @@ -55,6 +55,10 @@ namespace ShardingCore.DynamicDataSources using var context = serviceScope.ServiceProvider.GetService(); if (_entityConfigOptions.EnsureCreatedWithOutShardingTable || !isOnStart) EnsureCreated(virtualDataSource, context, dataSourceName); + else if(_entityConfigOptions.CreateDataBaseOnlyOnStart.GetValueOrDefault()) + { + EnsureCreateDataBaseOnly(context, dataSourceName); + } var tableEnsureManager = virtualDataSource.ConfigurationParams.TableEnsureManager; ////获取数据库存在的所有的表 var existTables = tableEnsureManager?.GetExistTables(context, dataSourceName) ?? new HashSet(); @@ -182,6 +186,33 @@ namespace ShardingCore.DynamicDataSources } } } + private void EnsureCreateDataBaseOnly( DbContext context, string dataSourceName) + { + if (context is IShardingDbContext shardingDbContext) + { + var dbContext = shardingDbContext.GetDbContext(dataSourceName, false, _routeTailFactory.Create(string.Empty)); + + + var modelCacheSyncObject = dbContext.GetModelCacheSyncObject(); + + var acquire = Monitor.TryEnter(modelCacheSyncObject, TimeSpan.FromSeconds(3)); + if (!acquire) + { + throw new ShardingCoreException("cant get modelCacheSyncObject lock"); + } + + try + { + dbContext.RemoveDbContextAllRelationModel(); + dbContext.Database.EnsureCreated(); + dbContext.RemoveModelCache(); + } + finally + { + Monitor.Exit(modelCacheSyncObject); + } + } + } } } diff --git a/src/ShardingCore/Extensions/DbContextExtension.cs b/src/ShardingCore/Extensions/DbContextExtension.cs index fe2892b8..0c2da234 100644 --- a/src/ShardingCore/Extensions/DbContextExtension.cs +++ b/src/ShardingCore/Extensions/DbContextExtension.cs @@ -140,6 +140,35 @@ namespace ShardingCore.Extensions } #endif } + /// + /// 移除所有的表 + /// + /// + public static void RemoveDbContextAllRelationModel(this DbContext dbContext) + { +#if EFCORE6 + + var contextModel = dbContext.GetService().Model; ; +#endif +#if EFCORE2 || EFCORE3 || EFCORE5 + + var contextModel = dbContext.Model as Model; +#endif + +#if EFCORE6 + var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel; + contextModelRelationalModel.Tables.Clear(); +#endif +#if EFCORE5 + var contextModelRelationalModel = contextModel.RelationalModel as RelationalModel; + contextModelRelationalModel.Tables.Clear(); +#endif +#if EFCORE2 || EFCORE3 + var entityTypes = + contextModel.GetFieldValue("_entityTypes") as SortedDictionary; + entityTypes.Clear(); +#endif + } /// /// 移除所有的除了我指定的那个类型 diff --git a/src/ShardingCore/TableCreator/ShardingTableCreator.cs b/src/ShardingCore/TableCreator/ShardingTableCreator.cs index 827d25aa..b6964c72 100644 --- a/src/ShardingCore/TableCreator/ShardingTableCreator.cs +++ b/src/ShardingCore/TableCreator/ShardingTableCreator.cs @@ -45,7 +45,6 @@ namespace ShardingCore.TableCreator /// /// /// - /// public void CreateTable(string dataSourceName, Type shardingEntityType, string tail) { using (var serviceScope = _serviceProvider.CreateScope()) diff --git a/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs index 8c2c8aea..e1b97793 100644 --- a/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs @@ -70,6 +70,7 @@ namespace ShardingCore.VirtualRoutes.Abstractions var allVirtualDataSources = virtualDataSourceManager.GetAllVirtualDataSources(); var now = DateTime.Now.AddMinutes(IncrementMinutes); var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now); + virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail)); foreach (var virtualDataSource in allVirtualDataSources) { ISet dataSources = new HashSet(); @@ -103,7 +104,6 @@ namespace ShardingCore.VirtualRoutes.Abstractions } } } - virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail)); return Task.CompletedTask; }