2021-03-22 11:12:00 +08:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2021-08-15 07:32:50 +08:00
|
|
|
using Sample.MySql.DbContexts;
|
2021-03-22 11:12:00 +08:00
|
|
|
using Sample.MySql.Domain.Entities;
|
2021-11-18 13:29:58 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2022-07-20 13:44:52 +08:00
|
|
|
using Microsoft.EntityFrameworkCore.Internal;
|
|
|
|
using Sample.MySql.multi;
|
2022-06-15 22:21:19 +08:00
|
|
|
using ShardingCore.Bootstrappers;
|
2021-03-22 11:12:00 +08:00
|
|
|
|
|
|
|
namespace Sample.MySql
|
|
|
|
{
|
2021-11-18 13:29:58 +08:00
|
|
|
/*
|
|
|
|
* @Author: xjm
|
|
|
|
* @Description:
|
|
|
|
* @Date: Tuesday, 26 January 2021 12:29:04
|
|
|
|
* @Email: 326308290@qq.com
|
|
|
|
*/
|
2021-03-22 11:12:00 +08:00
|
|
|
public static class DIExtension
|
|
|
|
{
|
|
|
|
public static void DbSeed(this IApplicationBuilder app)
|
|
|
|
{
|
|
|
|
using (var scope=app.ApplicationServices.CreateScope())
|
|
|
|
{
|
2021-10-11 20:58:55 +08:00
|
|
|
var virtualDbContext =scope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
2021-08-20 23:10:34 +08:00
|
|
|
if (!virtualDbContext.Set<SysUserMod>().Any())
|
2021-03-22 11:12:00 +08:00
|
|
|
{
|
|
|
|
var ids = Enumerable.Range(1, 1000);
|
|
|
|
var userMods = new List<SysUserMod>();
|
|
|
|
foreach (var id in ids)
|
|
|
|
{
|
|
|
|
userMods.Add(new SysUserMod()
|
|
|
|
{
|
|
|
|
Id = id.ToString(),
|
|
|
|
Age = id,
|
2022-07-04 22:47:03 +08:00
|
|
|
Name = $"ds{(id%3)}",
|
2021-03-22 11:12:00 +08:00
|
|
|
});
|
|
|
|
}
|
2021-03-22 14:43:36 +08:00
|
|
|
var userModMonths = new List<SysUserLogByMonth>();
|
|
|
|
foreach (var id in ids)
|
|
|
|
{
|
|
|
|
userModMonths.Add(new SysUserLogByMonth()
|
|
|
|
{
|
|
|
|
Id = id.ToString(),
|
|
|
|
Time = DateTime.Now
|
|
|
|
});
|
|
|
|
}
|
2021-03-22 11:12:00 +08:00
|
|
|
|
2021-08-15 07:32:50 +08:00
|
|
|
virtualDbContext.AddRange(userMods);
|
|
|
|
virtualDbContext.AddRange(userModMonths);
|
2021-03-22 11:12:00 +08:00
|
|
|
virtualDbContext.SaveChanges();
|
2022-02-27 23:10:28 +08:00
|
|
|
|
2021-03-22 11:12:00 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-20 13:44:52 +08:00
|
|
|
|
|
|
|
// using (var scope=app.ApplicationServices.CreateScope())
|
|
|
|
// {
|
|
|
|
//
|
|
|
|
// var virtualDbContext =scope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
|
|
|
// var any = virtualDbContext.Set<SysUserMod>().Any();
|
|
|
|
// }
|
2022-02-27 23:10:28 +08:00
|
|
|
//using (var scope = app.ApplicationServices.CreateScope())
|
|
|
|
//{
|
|
|
|
// var dbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
|
|
|
// var queryable = from sum in dbContext.Set<SysUserMod>()
|
|
|
|
// join st in dbContext.Set<SysTest>().Where(p => p.UserId == "admin")
|
|
|
|
// on sum.Id equals st.Id
|
|
|
|
// select new
|
|
|
|
// {
|
|
|
|
// st.UserId,
|
|
|
|
// sum.Name
|
|
|
|
// };
|
|
|
|
// var result = queryable.ToList();
|
|
|
|
|
|
|
|
// Console.WriteLine(result.Count);
|
|
|
|
//}
|
|
|
|
|
|
|
|
//using (var scope = app.ApplicationServices.CreateScope())
|
|
|
|
//{
|
|
|
|
// var dbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
|
|
|
|
|
|
|
// var queryable = from sum in dbContext.Set<SysUserMod>()
|
|
|
|
// from st in dbContext.Set<SysTest>().Where(p => p.Id == sum.Id && p.UserId == "admin")
|
|
|
|
// select new
|
|
|
|
// {
|
|
|
|
// st.UserId,
|
|
|
|
// sum.Name
|
|
|
|
// };
|
|
|
|
// var result = queryable.ToList();
|
|
|
|
|
|
|
|
// Console.WriteLine(result.Count);
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-22 11:12:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|