优化订单创建分表的方式
This commit is contained in:
parent
a0a223151d
commit
519fe66a06
|
@ -30,13 +30,13 @@ namespace Sample.SqlServer
|
|||
o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;")
|
||||
, op =>
|
||||
{
|
||||
op.EnsureCreatedWithOutShardingTable = true;
|
||||
op.CreateShardingTableOnStart = true;
|
||||
op.EnsureCreatedWithOutShardingTable = false;
|
||||
op.CreateShardingTableOnStart = false;
|
||||
op.UseShardingOptionsBuilder(
|
||||
(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
|
||||
));//使用链接字符串创建dbcontext
|
||||
);//使用链接字符串创建dbcontext
|
||||
op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
||||
});
|
||||
////不支持MARS不支持追踪的
|
||||
|
|
|
@ -22,27 +22,25 @@ namespace Samples.AutoByDate.SqlServer.Jobs
|
|||
/// </summary>
|
||||
/// <param name="virtualTableManager"></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)
|
||||
{
|
||||
var allVirtualTables = virtualTableManager.GetAllVirtualTables<DefaultShardingDbContext>();
|
||||
foreach (var virtualTable in allVirtualTables)
|
||||
var virtualTable = virtualTableManager.GetVirtualTable<DefaultShardingDbContext, SysUserLogByDay>();
|
||||
if (virtualTable == null)
|
||||
{
|
||||
if (virtualTable.EntityType == typeof(SysUserLogByDay))
|
||||
{
|
||||
var now = DateTime.Now.Date.AddDays(1);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
try
|
||||
{
|
||||
virtualTableManager.AddPhysicTable<DefaultShardingDbContext>(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||
tableCreator.CreateTable<DefaultShardingDbContext,SysUserLogByDay>(tail);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(1);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
try
|
||||
{
|
||||
virtualTableManager.AddPhysicTable<DefaultShardingDbContext>(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||
tableCreator.CreateTable<DefaultShardingDbContext, SysUserLogByDay>(tail);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue