2021-11-18 13:29:58 +08:00
|
|
|
|
using EFCore.BulkExtensions;
|
2021-01-26 14:39:56 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-03-10 14:17:41 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-08-15 07:32:50 +08:00
|
|
|
|
using Sample.SqlServer.DbContexts;
|
2021-01-26 14:39:56 +08:00
|
|
|
|
using Sample.SqlServer.Domain.Entities;
|
2021-08-25 19:02:44 +08:00
|
|
|
|
using ShardingCore.Core.QueryRouteManagers.Abstractions;
|
2021-01-26 14:39:56 +08:00
|
|
|
|
using ShardingCore.Extensions;
|
2021-10-03 14:09:01 +08:00
|
|
|
|
using ShardingCore.Extensions.ShardingPageExtensions;
|
2021-11-18 13:29:58 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-01-26 14:39:56 +08:00
|
|
|
|
|
|
|
|
|
namespace Sample.SqlServer.Controllers
|
|
|
|
|
{
|
2021-08-25 13:05:45 +08:00
|
|
|
|
public class STU
|
|
|
|
|
{
|
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
}
|
2021-01-26 14:39:56 +08:00
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("[controller]/[action]")]
|
|
|
|
|
public class ValuesController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
|
2021-08-16 04:21:46 +08:00
|
|
|
|
private readonly DefaultShardingDbContext _defaultTableDbContext;
|
2021-08-25 19:02:44 +08:00
|
|
|
|
private readonly IShardingRouteManager _shardingRouteManager;
|
2021-01-26 14:39:56 +08:00
|
|
|
|
|
2021-09-06 09:05:04 +08:00
|
|
|
|
public ValuesController(DefaultShardingDbContext defaultTableDbContext, IShardingRouteManager shardingRouteManager)
|
2021-01-26 14:39:56 +08:00
|
|
|
|
{
|
2021-08-15 07:32:50 +08:00
|
|
|
|
_defaultTableDbContext = defaultTableDbContext;
|
2021-08-25 19:02:44 +08:00
|
|
|
|
_shardingRouteManager = shardingRouteManager;
|
2021-01-26 14:39:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 11:03:25 +08:00
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> Get2x()
|
|
|
|
|
{
|
|
|
|
|
await _defaultTableDbContext.AddAsync(new SysUserMod()
|
|
|
|
|
{
|
|
|
|
|
Id = Guid.NewGuid().ToString("n"),
|
|
|
|
|
Age = 9,
|
|
|
|
|
AgeGroup = 10, Name = "SysUserModTest"
|
|
|
|
|
});
|
|
|
|
|
await _defaultTableDbContext.SaveChangesAsync();
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
2021-01-26 14:39:56 +08:00
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> Get()
|
|
|
|
|
{
|
2021-08-26 19:26:56 +08:00
|
|
|
|
var sql = from ut in _defaultTableDbContext.Set<SysTest>()
|
2021-09-06 09:05:04 +08:00
|
|
|
|
join u in _defaultTableDbContext.Set<SysUserMod>()
|
|
|
|
|
on ut.UserId equals u.Id
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
ut.Id,
|
|
|
|
|
userId = u.Id
|
|
|
|
|
};
|
|
|
|
|
var listAsync = await sql.ToListAsync();
|
2021-08-26 19:26:56 +08:00
|
|
|
|
var resultx112331tt = await _defaultTableDbContext.Set<SysTest>().CountAsync();
|
|
|
|
|
var resultx112331 = await _defaultTableDbContext.Set<SysUserMod>().CountAsync();
|
2021-09-06 09:05:04 +08:00
|
|
|
|
var resultx11233411 = _defaultTableDbContext.Set<SysUserMod>().Count();
|
2021-08-25 13:05:45 +08:00
|
|
|
|
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);
|
2021-08-18 14:09:56 +08:00
|
|
|
|
var resultx111 = await _defaultTableDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "198");
|
2021-08-25 13:05:45 +08:00
|
|
|
|
var resultx2 = await _defaultTableDbContext.Set<SysUserMod>().CountAsync(o => o.Age <= 10);
|
2021-08-17 22:17:18 +08:00
|
|
|
|
var resultx = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").FirstOrDefaultAsync();
|
2021-08-25 13:05:45 +08:00
|
|
|
|
var resultx33 = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Select(o => o.Id).FirstOrDefaultAsync();
|
2021-08-19 15:08:02 +08:00
|
|
|
|
var resulxxt = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").ToListAsync();
|
2021-08-17 22:17:18 +08:00
|
|
|
|
var result = await _defaultTableDbContext.Set<SysUserMod>().ToListAsync();
|
|
|
|
|
|
2021-08-19 15:08:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-09-03 21:23:26 +08:00
|
|
|
|
var sresultx11231 = _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Age == 198198).Select(o => o.Id).Contains("1981");
|
|
|
|
|
var sresultx1121 = _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Sum(o => o.Age);
|
|
|
|
|
var sresultx111 = _defaultTableDbContext.Set<SysUserMod>().FirstOrDefault(o => o.Id == "198");
|
|
|
|
|
var sresultx2 = _defaultTableDbContext.Set<SysUserMod>().Count(o => o.Age <= 10);
|
|
|
|
|
var sresultx = _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").FirstOrDefault();
|
|
|
|
|
var sresultx33 = _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Select(o => o.Id).FirstOrDefault();
|
|
|
|
|
var sresultxc = _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Select(o => o.Id).ToList();
|
|
|
|
|
var sresultxasdc = _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").ToList();
|
|
|
|
|
var sresult = _defaultTableDbContext.Set<SysUserMod>().ToList();
|
2021-09-22 21:55:52 +08:00
|
|
|
|
var sysUserMod98 = result.FirstOrDefault(o => o.Id == "98");
|
|
|
|
|
sysUserMod98.Name = "name_update" + new Random().Next(1, 99) + "_98";
|
2021-10-19 17:08:36 +08:00
|
|
|
|
using (var tran = _defaultTableDbContext.Database.BeginTransaction())
|
|
|
|
|
{
|
|
|
|
|
await _defaultTableDbContext.SaveChangesAsync();
|
|
|
|
|
tran.Commit();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 19:02:44 +08:00
|
|
|
|
var stu = new STU() { Id = "198" };
|
2021-08-25 13:05:45 +08:00
|
|
|
|
var sresultx111x = _defaultTableDbContext.Set<SysUserMod>().FirstOrDefault(o => o.Id == stu.Id);
|
|
|
|
|
|
|
|
|
|
var pageResult = await _defaultTableDbContext.Set<SysUserMod>().Skip(10).Take(10).OrderBy(o => o.Age).ToListAsync();
|
2021-08-25 19:02:44 +08:00
|
|
|
|
|
2021-10-08 13:37:02 +08:00
|
|
|
|
var resuxaasa = await _defaultTableDbContext.Set<SysTest>().ToListAsync();
|
2021-08-25 19:02:44 +08:00
|
|
|
|
|
|
|
|
|
using (_shardingRouteManager.CreateScope())
|
|
|
|
|
{
|
2021-11-12 22:39:11 +08:00
|
|
|
|
_shardingRouteManager.Current.TryCreateOrAddMustTail<SysUserMod>("00","01");
|
|
|
|
|
//_shardingRouteManager.Current.TryCreateOrAddHintTail<SysUserMod>("00", "01");
|
|
|
|
|
//_shardingRouteManager.Current.TryCreateOrAddAssertTail<SysUserMod>(new TestRouteAssert());
|
2021-08-25 19:02:44 +08:00
|
|
|
|
|
|
|
|
|
var mod00s = await _defaultTableDbContext.Set<SysUserMod>().Skip(10).Take(11).ToListAsync();
|
|
|
|
|
}
|
2021-09-22 21:55:52 +08:00
|
|
|
|
//_defaultTableDbContext.RemoveRange(_defaultTableDbContext.Set<SysUserMod>());
|
|
|
|
|
//await _defaultTableDbContext.SaveChangesAsync();
|
2021-08-25 19:02:44 +08:00
|
|
|
|
return Ok();
|
2021-01-26 14:39:56 +08:00
|
|
|
|
}
|
2021-09-03 16:00:12 +08:00
|
|
|
|
[HttpGet]
|
2021-09-06 09:05:04 +08:00
|
|
|
|
public async Task<IActionResult> Get1([FromQuery] int p, [FromQuery] int s)
|
2021-09-03 16:00:12 +08:00
|
|
|
|
{
|
2021-09-03 21:23:26 +08:00
|
|
|
|
Stopwatch sp = new Stopwatch();
|
|
|
|
|
sp.Start();
|
2021-09-06 09:05:04 +08:00
|
|
|
|
var shardingPageResultAsync = await _defaultTableDbContext.Set<SysUserMod>().OrderBy(o => o.Age).ToShardingPageAsync(p, s);
|
2021-09-03 21:23:26 +08:00
|
|
|
|
sp.Stop();
|
|
|
|
|
return Ok(new
|
|
|
|
|
{
|
|
|
|
|
sp.ElapsedMilliseconds,
|
|
|
|
|
shardingPageResultAsync
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
[HttpGet]
|
2021-09-06 09:05:04 +08:00
|
|
|
|
public IActionResult Get2([FromQuery] int p, [FromQuery] int s)
|
2021-09-03 21:23:26 +08:00
|
|
|
|
{
|
|
|
|
|
Stopwatch sp = new Stopwatch();
|
|
|
|
|
sp.Start();
|
2021-09-06 09:05:04 +08:00
|
|
|
|
var shardingPageResultAsync = _defaultTableDbContext.Set<SysUserMod>().OrderBy(o => o.Age).ToShardingPage(p, s);
|
2021-09-03 21:23:26 +08:00
|
|
|
|
sp.Stop();
|
|
|
|
|
return Ok(new
|
|
|
|
|
{
|
|
|
|
|
sp.ElapsedMilliseconds,
|
|
|
|
|
shardingPageResultAsync
|
|
|
|
|
});
|
2021-09-03 16:00:12 +08:00
|
|
|
|
}
|
2021-11-05 21:03:54 +08:00
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult Get3()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//var dbContext2s = _defaultTableDbContext.BulkShardingExpression<SysUserMod>(o => o.Age > 100);
|
|
|
|
|
//using (var tran = _defaultTableDbContext.Database.BeginTransaction())
|
|
|
|
|
//{
|
|
|
|
|
// dbContext2s.ForEach(dbContext =>
|
|
|
|
|
// {
|
|
|
|
|
// dbContext.Set<SysUserMod>().Where(o => o.Age > 100).Update(o => new SysUserMod()
|
|
|
|
|
// {
|
|
|
|
|
// AgeGroup = 1000
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
// _defaultTableDbContext.SaveChanges();
|
|
|
|
|
// tran.Commit();
|
|
|
|
|
//}
|
|
|
|
|
var list = new List<SysUserMod>();
|
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
|
|
|
{
|
|
|
|
|
list.Add(new SysUserMod()
|
|
|
|
|
{
|
|
|
|
|
Id =i.ToString(),
|
|
|
|
|
Name = i.ToString(),
|
|
|
|
|
Age = i,
|
|
|
|
|
AgeGroup = i
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var tran = _defaultTableDbContext.Database.BeginTransaction())
|
|
|
|
|
{
|
|
|
|
|
var dbContexts = _defaultTableDbContext.BulkShardingTableEnumerable(list);
|
|
|
|
|
dbContexts.ForEach(kv =>
|
|
|
|
|
{
|
|
|
|
|
kv.Key.BulkInsert(kv.Value.ToList());
|
|
|
|
|
});
|
|
|
|
|
var a = 0;
|
|
|
|
|
var b = 1 / a;
|
|
|
|
|
tran.Commit();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 13:29:58 +08:00
|
|
|
|
|
2021-11-05 21:03:54 +08:00
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
2021-09-06 09:05:04 +08:00
|
|
|
|
|
2021-01-26 14:39:56 +08:00
|
|
|
|
}
|
2021-09-06 09:05:04 +08:00
|
|
|
|
}
|