ShardingCore呈现“完美”分表(2)

public class DefaultShardingDbContext:AbstractShardingDbContext<DefaultTableDbContext> { public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.ApplyConfiguration(new SysUserModMap()); } public override Type ShardingDbContextType => this.GetType(); }

6.添加配置

public void ConfigureServices(IServiceCollection services) { services.AddControllers(); //原先的dbcontext可以用也可以不用如果原先的dbcontext还在用就继续 //services.AddDbContext<DefaultTableDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx3;Integrated Security=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).UseLoggerFactory(efLogger),//使用dbconnection创建dbcontext支持事务 (conStr,builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger));//使用链接字符串创建dbcontext op.AddShardingTableRoute<SysUserModVirtualTableRoute>(); }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { ... //添加启动项 app.UseShardingCore(); ... } public static class ShardingCoreExtension{ public static IApplicationBuilder UseShardingCore(this IApplicationBuilder app) { var shardingBootstrapper = app.ApplicationServices.GetRequiredService<IShardingBootstrapper>(); shardingBootstrapper.Start(); return app; } }

7.控制器使用

private readonly DefaultShardingDbContext _defaultTableDbContext; public ValuesController(DefaultShardingDbContext defaultTableDbContext) { _defaultTableDbContext = defaultTableDbContext; } [HttpGet] public async Task<IActionResult> Get() { var resultx11231 = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Age == 198198).Select(o=>o.Id).ContainsAsync("1981"); var resultx1121 = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").SumAsync(o=>o.Age); var resultx111 = await _defaultTableDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "198"); var resultx2 = await _defaultTableDbContext.Set<SysUserMod>().CountAsync(o => o.Age<=10); var resultx = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").FirstOrDefaultAsync(); var resultx33 = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Select(o=>o.Id).FirstOrDefaultAsync(); var resulxxt = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").ToListAsync(); var result = await _defaultTableDbContext.Set<SysUserMod>().ToListAsync(); var sysUserMod98 = result.FirstOrDefault(o => o.Id == "98"); _defaultTableDbContext.Attach(sysUserMod98); sysUserMod98.Name = "name_update"+new Random().Next(1,99)+"_98"; await _defaultTableDbContext.SaveChangesAsync(); return Ok(result); }

自定义分表键,自定义分表规则

目前市面上有的框架要么对分表字段有限制比如仅支持DateTime类型或者int等,要么对分表规则有限制:仅支持按天、按月、取模...等等,但是基于分表规则和分表字段是业务规则所以本框架遵循将其由业务系统自己定义,最大化来实现分表库的适用性,基本上满足一切分表规则,且sharding-core目前默认提供一些常用的分表规则可以快速集成。

默认路由

抽象abstract 路由规则 tail 索引
AbstractSimpleShardingModKeyIntVirtualTableRoute   取模   0,1,2...   =  
AbstractSimpleShardingModKeyStringVirtualTableRoute   取模   0,1,2...   =  
AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute   按时间   yyyyMMdd   >,>=,<,<=,=,contains  
AbstractSimpleShardingDayKeyLongVirtualTableRoute   按时间戳   yyyyMMdd   >,>=,<,<=,=,contains  
AbstractSimpleShardingWeekKeyDateTimeVirtualTableRoute   按时间   yyyyMMdd_dd   >,>=,<,<=,=,contains  
AbstractSimpleShardingWeekKeyLongVirtualTableRoute   按时间戳   yyyyMMdd_dd   >,>=,<,<=,=,contains  
AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute   按时间   yyyyMM   >,>=,<,<=,=,contains  
AbstractSimpleShardingMonthKeyLongVirtualTableRoute   按时间戳   yyyyMM   >,>=,<,<=,=,contains  
AbstractSimpleShardingYearKeyDateTimeVirtualTableRoute   按时间   yyyy   >,>=,<,<=,=,contains  
AbstractSimpleShardingYearKeyLongVirtualTableRoute   按时间戳   yyyy   >,>=,<,<=,=,contains  

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zysfgg.html