优化订单创建分表的方式
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;")
|
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不支持追踪的
|
||||||
|
|
|
@ -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
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue