2021-09-03 16:00:12 +08:00
|
|
|
using System;
|
2021-03-08 17:33:04 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2021-01-26 14:39:56 +08:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2021-09-03 16:00:12 +08:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-01-26 14:39:56 +08:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2021-08-15 07:32:50 +08:00
|
|
|
using Sample.SqlServer.DbContexts;
|
2021-03-08 17:33:04 +08:00
|
|
|
using Sample.SqlServer.Domain.Entities;
|
2021-01-26 14:39:56 +08:00
|
|
|
using ShardingCore;
|
2021-10-29 10:44:48 +08:00
|
|
|
using ShardingCore.Bootstrapers;
|
2021-01-26 14:39:56 +08:00
|
|
|
|
|
|
|
namespace Sample.SqlServer
|
|
|
|
{
|
2021-08-20 13:50:49 +08:00
|
|
|
/*
|
|
|
|
* @Author: xjm
|
|
|
|
* @Description:
|
|
|
|
* @Date: Tuesday, 26 January 2021 12:29:04
|
|
|
|
* @Email: 326308290@qq.com
|
|
|
|
*/
|
2021-01-26 14:39:56 +08:00
|
|
|
public static class DIExtension
|
|
|
|
{
|
|
|
|
public static IApplicationBuilder UseShardingCore(this IApplicationBuilder app)
|
|
|
|
{
|
|
|
|
var shardingBootstrapper = app.ApplicationServices.GetRequiredService<IShardingBootstrapper>();
|
|
|
|
shardingBootstrapper.Start();
|
|
|
|
return app;
|
|
|
|
}
|
2021-03-08 17:33:04 +08:00
|
|
|
|
|
|
|
public static void DbSeed(this IApplicationBuilder app)
|
|
|
|
{
|
2021-09-03 16:00:12 +08:00
|
|
|
using (var scope = app.ApplicationServices.CreateScope())
|
2021-03-08 17:33:04 +08:00
|
|
|
{
|
2021-09-03 16:00:12 +08:00
|
|
|
var virtualDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
2021-08-20 13:50:49 +08:00
|
|
|
if (!virtualDbContext.Set<SysUserMod>().Any())
|
|
|
|
{
|
|
|
|
var ids = Enumerable.Range(1, 1000);
|
|
|
|
var userMods = new List<SysUserMod>();
|
2021-09-03 16:00:12 +08:00
|
|
|
var userSalaries = new List<SysUserSalary>();
|
2021-08-26 19:26:56 +08:00
|
|
|
var SysTests = new List<SysTest>();
|
2021-09-03 16:00:12 +08:00
|
|
|
var beginTime = new DateTime(2020, 1, 1);
|
|
|
|
var endTime = new DateTime(2021, 12, 1);
|
2021-08-20 13:50:49 +08:00
|
|
|
foreach (var id in ids)
|
|
|
|
{
|
|
|
|
userMods.Add(new SysUserMod()
|
|
|
|
{
|
|
|
|
Id = id.ToString(),
|
|
|
|
Age = id,
|
|
|
|
Name = $"name_{id}",
|
2021-09-03 16:00:12 +08:00
|
|
|
AgeGroup = Math.Abs(id % 10)
|
2021-08-20 13:50:49 +08:00
|
|
|
});
|
2021-08-26 19:26:56 +08:00
|
|
|
SysTests.Add(new SysTest()
|
|
|
|
{
|
|
|
|
Id = id.ToString(),
|
|
|
|
UserId = id.ToString()
|
|
|
|
});
|
2021-09-03 16:00:12 +08:00
|
|
|
var tempTime = beginTime;
|
|
|
|
var i = 0;
|
|
|
|
while (tempTime <= endTime)
|
|
|
|
{
|
|
|
|
var dateOfMonth = $@"{tempTime:yyyyMM}";
|
|
|
|
userSalaries.Add(new SysUserSalary()
|
|
|
|
{
|
|
|
|
Id = $@"{id}{dateOfMonth}",
|
|
|
|
UserId = id.ToString(),
|
|
|
|
DateOfMonth = int.Parse(dateOfMonth),
|
|
|
|
Salary = 700000 + id * 100 * i,
|
|
|
|
SalaryLong = 700000 + id * 100 * i,
|
|
|
|
SalaryDecimal = (700000 + id * 100 * i) / 100m,
|
|
|
|
SalaryDouble = (700000 + id * 100 * i) / 100d,
|
|
|
|
SalaryFloat = (700000 + id * 100 * i) / 100f
|
|
|
|
});
|
|
|
|
tempTime = tempTime.AddMonths(1);
|
|
|
|
i++;
|
|
|
|
}
|
2021-08-20 13:50:49 +08:00
|
|
|
}
|
2021-03-08 17:33:04 +08:00
|
|
|
|
2021-09-03 16:00:12 +08:00
|
|
|
|
2021-10-20 11:08:44 +08:00
|
|
|
|
|
|
|
using (var tran = virtualDbContext.Database.BeginTransaction())
|
|
|
|
{
|
|
|
|
virtualDbContext.AddRange(userMods);
|
|
|
|
virtualDbContext.AddRange(SysTests);
|
|
|
|
virtualDbContext.AddRange(userSalaries);
|
|
|
|
|
|
|
|
virtualDbContext.SaveChanges();
|
|
|
|
tran.Commit();
|
|
|
|
}
|
2021-08-20 13:50:49 +08:00
|
|
|
}
|
2021-03-08 17:33:04 +08:00
|
|
|
}
|
|
|
|
}
|
2021-01-26 14:39:56 +08:00
|
|
|
}
|
|
|
|
}
|