基于efcore的分表组件开源 (2)

Startup.cs 下的 ConfigureServices(IServiceCollection services)

services.AddShardingSqlServer(o => { o.ConnectionString = ""; o.AddSharding<SysUserModVirtualRoute>(); o.UseShardingCoreConfig((provider, config) => { //如果是development就判断并且新建数据库如果不存在的话(ishardingentity不会被创建) config.EnsureCreated = provider.GetService<IHostEnvironment>().IsDevelopment(); //ishardingentity表是否需要在启动时创建(如果已创建可以选择不创建) config.CreateShardingTableOnStart = true; }); });

Startup.cs 下的 Configure(IApplicationBuilder app, IWebHostEnvironment env) 你也可以自行封装app.UseShardingCore()

var shardingBootstrapper = app.ApplicationServices.GetRequiredService<IShardingBootstrapper>(); shardingBootstrapper.Start(); 使用 private readonly IVirtualDbContext _virtualDbContext; public ctor(IVirtualDbContext virtualDbContext) { _virtualDbContext = virtualDbContext; } public async Task ToList_All() { //查询list集合 var all=await _virtualDbContext.Set<SysUserMod>().ToShardingListAsync(); //链接查询 var list = await (from u in _virtualDbContext.Set<SysUserMod>() join salary in _virtualDbContext.Set<SysUserSalary>() on u.Id equals salary.UserId select new { Salary = salary.Salary, DateOfMonth = salary.DateOfMonth, Name = u.Name }).ToShardingListAsync(); //聚合查询 var ids = new[] {"200", "300"}; var dateOfMonths = new[] {202111, 202110}; var group = await (from u in _virtualDbContext.Set<SysUserSalary>() .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth)) group u by new { UId = u.UserId } into g select new { GroupUserId = g.Key.UId, Count = g.Count(), TotalSalary = g.Sum(o => o.Salary), AvgSalary = g.Average(o => o.Salary), MinSalary = g.Min(o => o.Salary), MaxSalary = g.Max(o => o.Salary) }).ToShardingListAsync(); }

更多操作可以参考单元测试

Api 方法 Method SqlServer Unit Test MySql Unit Test
获取集合   ToShardingListAsync   yes   yes  
第一条   ShardingFirstOrDefaultAsync   yes   yes  
最大   ShardingMaxAsync   yes   yes  
最小   ShardingMinAsync   yes   yes  
是否存在   ShardingAnyAsync   yes   yes  
分页   ToShardingPageResultAsync   yes   yes  
数目   ShardingCountAsync   yes   yes  
求和   ShardingSumAsync   yes   yes  
分组   ShardingGroupByAsync   yes   yes  
默认路由 抽象abstract 路由规则 tail 索引
AbstractSimpleShardingModKeyIntVirtualRoute   取模   0,1,2...   =  
AbstractSimpleShardingModKeyStringVirtualRoute   取模   0,1,2...   =  
AbstractSimpleShardingDayKeyDateTimeVirtualRoute   按时间   yyyyMMdd   >,>=,<,<=,=,contains  
AbstractSimpleShardingDayKeyLongVirtualRoute   按时间戳   yyyyMMdd   >,>=,<,<=,=,contains  
AbstractSimpleShardingWeekKeyDateTimeVirtualRoute   按时间   yyyyMMdd_dd   >,>=,<,<=,=,contains  
AbstractSimpleShardingWeekKeyLongVirtualRoute   按时间戳   yyyyMMdd_dd   >,>=,<,<=,=,contains  
AbstractSimpleShardingMonthKeyDateTimeVirtualRoute   按时间   yyyyMM   >,>=,<,<=,=,contains  
AbstractSimpleShardingMonthKeyLongVirtualRoute   按时间戳   yyyyMM   >,>=,<,<=,=,contains  
AbstractSimpleShardingYearKeyDateTimeVirtualRoute   按时间   yyyy   >,>=,<,<=,=,contains  
AbstractSimpleShardingYearKeyLongVirtualRoute   按时间戳   yyyy   >,>=,<,<=,=,contains  

注:contains表示为o=>ids.contains(o.shardingkey)

高级 批量操作

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

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