demo auto by date
This commit is contained in:
parent
184f45e119
commit
87f60729f4
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Samples.AutoByDate.SqlServer.Domain.Maps;
|
||||
using ShardingCore.Sharding;
|
||||
|
||||
namespace Samples.AutoByDate.SqlServer.DbContexts
|
||||
{
|
||||
public class DefaultShardingDbContext:AbstractShardingDbContext<DefaultTableDbContext>
|
||||
{
|
||||
public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new SysUserLogByDayMap());
|
||||
}
|
||||
|
||||
public override Type ShardingDbContextType => this.GetType();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Samples.AutoByDate.SqlServer.Domain.Maps;
|
||||
using ShardingCore.Core.VirtualRoutes.Abstractions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Samples.AutoByDate.SqlServer.DbContexts
|
||||
{
|
||||
public class DefaultTableDbContext: DbContext,IShardingTableDbContext
|
||||
{
|
||||
public DefaultTableDbContext(DbContextOptions<DefaultTableDbContext> options) :base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new SysUserLogByDayMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using ChronusJob.Abstractions;
|
||||
using ChronusJob.Jobs.Attributes;
|
||||
using Samples.AutoByDate.SqlServer.DbContexts;
|
||||
using Samples.AutoByDate.SqlServer.Domain.Entities;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
|
@ -14,35 +15,35 @@ namespace Samples.AutoByDate.SqlServer.Jobs
|
|||
* @Date: Tuesday, 02 February 2021 17:24:17
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
//public class AutoCreateTableByDay : IJob
|
||||
//{
|
||||
// /// <summary>
|
||||
// /// 每天中午12点执行,启动的时候执行以下
|
||||
// /// </summary>
|
||||
// /// <param name="virtualTableManager"></param>
|
||||
// /// <param name="tableCreator"></param>
|
||||
// [JobRun(Name = "定时创建分表组件",Cron = "0 0 12 * * ?",RunOnceOnStart = true)]
|
||||
public class AutoCreateTableByDay : IJob
|
||||
{
|
||||
/// <summary>
|
||||
/// 每天中午12点执行,启动的时候执行以下
|
||||
/// </summary>
|
||||
/// <param name="virtualTableManager"></param>
|
||||
/// <param name="tableCreator"></param>
|
||||
[JobRun(Name = "定时创建分表组件",Cron = "0 0 12 * * ?",RunOnceOnStart = true)]
|
||||
|
||||
// public void AutoCreateTable(IVirtualTableManager virtualTableManager, IShardingTableCreator tableCreator)
|
||||
// {
|
||||
// var allVirtualTables = virtualTableManager.GetAllVirtualTables();
|
||||
// foreach (var virtualTable in allVirtualTables)
|
||||
// {
|
||||
// if (virtualTable.EntityType == typeof(SysUserLogByDay))
|
||||
// {
|
||||
// var now = DateTime.Now.Date.AddDays(1);
|
||||
// var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
// try
|
||||
// {
|
||||
// virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||
// tableCreator.CreateTable<SysUserLogByDay>(tail);
|
||||
// }
|
||||
// catch (Exception e)
|
||||
// {
|
||||
// //ignore
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
public void AutoCreateTable(IVirtualTableManager virtualTableManager, IShardingTableCreator tableCreator)
|
||||
{
|
||||
var allVirtualTables = virtualTableManager.GetAllVirtualTables<DefaultShardingDbContext>();
|
||||
foreach (var virtualTable in allVirtualTables)
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@
|
|||
<PackageReference Include="ChronusJob" Version="1.0.5" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.1" NoWarn="NU1605" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.1" NoWarn="NU1605" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.9" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -7,11 +7,15 @@ using Microsoft.AspNetCore.Builder;
|
|||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Samples.AutoByDate.SqlServer.DbContexts;
|
||||
using Samples.AutoByDate.SqlServer.Shardings;
|
||||
using ShardingCore;
|
||||
|
||||
namespace Samples.AutoByDate.SqlServer
|
||||
{
|
||||
|
@ -29,17 +33,17 @@ namespace Samples.AutoByDate.SqlServer
|
|||
{
|
||||
services.AddControllers();
|
||||
services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo {Title = "Samples.AutoByDate.SqlServer", Version = "v1"}); });
|
||||
//services.AddShardingSqlServer(o =>
|
||||
//{
|
||||
// o.ConnectionString = "";
|
||||
// o.AddSharding<SysUserLogByDayVirtualTableRoute>();
|
||||
// o.UseShardingCoreConfig((provider, config) =>
|
||||
// {
|
||||
// //如果是development就判断并且新建数据库如果不存在的话
|
||||
// config.EnsureCreated = provider.GetService<IHostEnvironment>().IsDevelopment();
|
||||
// config.CreateShardingTableOnStart = true;
|
||||
// });
|
||||
//});
|
||||
|
||||
services.AddShardingDbContext<DefaultShardingDbContext, DefaultTableDbContext>(
|
||||
o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx2;Integrated Security=True;")
|
||||
, op =>
|
||||
{
|
||||
op.EnsureCreatedWithOutShardingTable = true;
|
||||
op.CreateShardingTableOnStart = true;
|
||||
op.UseShardingOptionsBuilder((connection, builder) => builder.UseSqlServer(connection),
|
||||
(conStr,builder) => builder.UseSqlServer(conStr));
|
||||
op.AddShardingTableRoute<SysUserLogByDayVirtualTableRoute>();
|
||||
});
|
||||
services.AddChronusJob();
|
||||
}
|
||||
|
||||
|
@ -54,7 +58,6 @@ namespace Samples.AutoByDate.SqlServer
|
|||
}
|
||||
|
||||
app.UseShardingCore();
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
|
|
Loading…
Reference in New Issue