优化订单创建分表的方式

This commit is contained in:
xuejiaming 2021-08-27 10:06:29 +08:00
parent a0a223151d
commit 519fe66a06
2 changed files with 19 additions and 21 deletions

View File

@ -30,13 +30,13 @@ namespace Sample.SqlServer
o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;") o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;")
, op => , op =>
{ {
op.EnsureCreatedWithOutShardingTable = true; op.EnsureCreatedWithOutShardingTable = false;
op.CreateShardingTableOnStart = true; op.CreateShardingTableOnStart = false;
op.UseShardingOptionsBuilder( op.UseShardingOptionsBuilder(
(connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger),//使用dbconnection创建dbcontext支持事务 (connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger),//使用dbconnection创建dbcontext支持事务
(conStr,builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger (conStr,builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger)
//.ReplaceService<IQueryTranslationPostprocessorFactory,SqlServer2008QueryTranslationPostprocessorFactory>()//支持sqlserver2008r2 //.ReplaceService<IQueryTranslationPostprocessorFactory,SqlServer2008QueryTranslationPostprocessorFactory>()//支持sqlserver2008r2
));//使用链接字符串创建dbcontext );//使用链接字符串创建dbcontext
op.AddShardingTableRoute<SysUserModVirtualTableRoute>(); op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
}); });
////不支持MARS不支持追踪的 ////不支持MARS不支持追踪的

View File

@ -22,27 +22,25 @@ namespace Samples.AutoByDate.SqlServer.Jobs
/// </summary> /// </summary>
/// <param name="virtualTableManager"></param> /// <param name="virtualTableManager"></param>
/// <param name="tableCreator"></param> /// <param name="tableCreator"></param>
[JobRun(Name = "定时创建分表组件",Cron = "0 0 12 * * ?",RunOnceOnStart = true)] [JobRun(Name = "定时创建分表组件", Cron = "0 0 12 * * ?", RunOnceOnStart = true)]
public void AutoCreateTable(IVirtualTableManager virtualTableManager, IShardingTableCreator tableCreator) public void AutoCreateTable(IVirtualTableManager virtualTableManager, IShardingTableCreator tableCreator)
{ {
var allVirtualTables = virtualTableManager.GetAllVirtualTables<DefaultShardingDbContext>(); var virtualTable = virtualTableManager.GetVirtualTable<DefaultShardingDbContext, SysUserLogByDay>();
foreach (var virtualTable in allVirtualTables) if (virtualTable == null)
{ {
if (virtualTable.EntityType == typeof(SysUserLogByDay)) return;
{ }
var now = DateTime.Now.Date.AddDays(1); var now = DateTime.Now.Date.AddDays(1);
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now); var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
try try
{ {
virtualTableManager.AddPhysicTable<DefaultShardingDbContext>(virtualTable, new DefaultPhysicTable(virtualTable, tail)); virtualTableManager.AddPhysicTable<DefaultShardingDbContext>(virtualTable, new DefaultPhysicTable(virtualTable, tail));
tableCreator.CreateTable<DefaultShardingDbContext,SysUserLogByDay>(tail); tableCreator.CreateTable<DefaultShardingDbContext, SysUserLogByDay>(tail);
} }
catch (Exception e) catch (Exception e)
{ {
//ignore //ignore
}
}
} }
} }
} }