perfect for building test 1

This commit is contained in:
xuejiaming 2021-08-16 04:21:46 +08:00
parent 9a53dcd684
commit f3aa01e999
15 changed files with 120 additions and 83 deletions

View File

@ -16,9 +16,9 @@ namespace Sample.SqlServer.Controllers
public class ValuesController : ControllerBase
{
private readonly DefaultTableDbContext _defaultTableDbContext;
private readonly DefaultShardingDbContext _defaultTableDbContext;
public ValuesController(DefaultTableDbContext defaultTableDbContext)
public ValuesController(DefaultShardingDbContext defaultTableDbContext)
{
_defaultTableDbContext = defaultTableDbContext;
}
@ -26,9 +26,8 @@ namespace Sample.SqlServer.Controllers
[HttpGet]
public async Task<IActionResult> Get()
{
var result = await _defaultTableDbContext.Set<SysTest>().AnyAsync();
var result1 = await _defaultTableDbContext.Set<SysUserMod>().Where(o=>o.Id=="2"||o.Id=="3").ToShardingListAsync();
return Ok(result1);
var result = await _defaultTableDbContext.Set<SysUserMod>().OrderBy(o=>o.Age).ToListAsync();
return Ok(result);
}
}
}

View File

@ -31,23 +31,23 @@ namespace Sample.SqlServer
using (var scope=app.ApplicationServices.CreateScope())
{
var virtualDbContext =scope.ServiceProvider.GetService<DefaultTableDbContext>();
if (!virtualDbContext.Set<SysUserMod>().ShardingAny())
{
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,
Name = $"name_{id}",
});
}
//if (!virtualDbContext.Set<SysUserMod>().ShardingAny())
//{
// 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,
// Name = $"name_{id}",
// });
// }
virtualDbContext.AddRange(userMods);
virtualDbContext.SaveChanges();
}
// virtualDbContext.AddRange(userMods);
// virtualDbContext.SaveChanges();
//}
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Sample.SqlServer.Domain.Maps;
using ShardingCore.Sharding;
namespace Sample.SqlServer.DbContexts
{
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());
modelBuilder.ApplyConfiguration(new SysTestMap());
}
}
}

View File

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Sample.SqlServer.Domain.Maps;
using ShardingCore.DbContexts.ShardingDbContexts;
using ShardingCore.Sharding.Abstractions;
namespace Sample.SqlServer.DbContexts
{

View File

@ -14,7 +14,7 @@ namespace Sample.SqlServer.Domain.Entities
/// <summary>
/// 用户Id用于分表
/// </summary>
[ShardingTableKey(TailPrefix = "")]
[ShardingTableKey(TailPrefix = "_")]
public string Id { get; set; }
/// <summary>
/// 用户名称

View File

@ -20,7 +20,7 @@
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "values/get",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

View File

@ -32,8 +32,11 @@ namespace Sample.SqlServer
//o.AddDataSourceVirtualRoute<>();
});
services.AddDbContext<DefaultTableDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB123;Integrated Security=True")
.UseShardingSqlServerUpdateSqlGenerator());
services.AddDbContext<DefaultTableDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True"));
services.AddDbContext<DefaultShardingDbContext>(o =>
o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;").UseSharding());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@ -49,7 +52,7 @@ namespace Sample.SqlServer
app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
app.DbSeed();
//app.DbSeed();
}
}
}

View File

@ -38,6 +38,7 @@ namespace ShardingCore.SqlServer
}
public DbContextOptions GetDbContextOptions(DbConnection dbConnection)
{
Console.WriteLine("create new dbcontext options,dbconnection is new:"+(dbConnection==null));
var track = dbConnection != null;
var connection = dbConnection ?? GetSqlConnection();

View File

@ -37,9 +37,6 @@ namespace ShardingCore
//services.AddSingleton(typeof(IVirtualTable<>), typeof(OneDbVirtualTable<>));
services.AddSingleton<IShardingAccessor, ShardingAccessor>();
services.AddSingleton<IShardingScopeFactory, ShardingScopeFactory>();
//分表
services.AddSingleton<IShardingTableAccessor, ShardingTableAccessor>();
services.AddSingleton<IShardingTableScopeFactory, ShardingTableScopeFactory>();
return services;
}
}

View File

@ -21,14 +21,12 @@ namespace ShardingCore.DbContexts
public class ShardingDbContextFactory:IShardingDbContextFactory
{
private readonly IShardingCoreOptions _shardingCoreOptions;
private readonly IShardingTableScopeFactory _shardingTableScopeFactory;
private readonly IDbContextCreateFilterManager _dbContextCreateFilterManager;
private readonly IDbContextOptionsProvider _dbContextOptionsProvider;
public ShardingDbContextFactory(IShardingCoreOptions shardingCoreOptions,IShardingTableScopeFactory shardingTableScopeFactory, IDbContextCreateFilterManager dbContextCreateFilterManager,IDbContextOptionsProvider dbContextOptionsProvider)
public ShardingDbContextFactory(IShardingCoreOptions shardingCoreOptions, IDbContextCreateFilterManager dbContextCreateFilterManager,IDbContextOptionsProvider dbContextOptionsProvider)
{
_shardingCoreOptions = shardingCoreOptions;
_shardingTableScopeFactory = shardingTableScopeFactory;
_dbContextCreateFilterManager = dbContextCreateFilterManager;
_dbContextOptionsProvider = dbContextOptionsProvider;
}

View File

@ -29,34 +29,30 @@ namespace ShardingCore.EFCores
public override void Customize(ModelBuilder modelBuilder, DbContext context)
{
base.Customize(modelBuilder, context);
if (context is IShardingTableDbContext)
if (context is IShardingTableDbContext shardingTableDbContext)
{
var shardingTableAccessor = ShardingContainer.Services.GetService<IShardingTableAccessor>();
if (shardingTableAccessor.Context != null)
{
if (!string.IsNullOrWhiteSpace(shardingTableAccessor.Context.Tail))
{
var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
var tail = shardingTableAccessor.Context.Tail;
var typeMap = virtualTableManager.GetAllVirtualTables().Where(o=>o.GetTaleAllTails().Contains(tail)).Select(o=>o.EntityType).ToHashSet();
var tail = shardingTableDbContext.GetShardingTableDbContextTail();
//设置分表
var mutableEntityTypes = modelBuilder.Model.GetEntityTypes().Where(o => o.ClrType.IsShardingTable()&&typeMap.Contains(o.ClrType));
foreach (var entityType in mutableEntityTypes)
{
var shardingEntityConfig = ShardingKeyUtil.Parse(entityType.ClrType);
var shardingEntity = shardingEntityConfig.ShardingEntityType;
var tailPrefix = shardingEntityConfig.TailPrefix;
var entity = modelBuilder.Entity(shardingEntity);
var tableName = shardingEntityConfig.ShardingOriginalTable;
if (string.IsNullOrWhiteSpace(tableName))
throw new ArgumentNullException($"{shardingEntity}: not found original table name。");
if (!string.IsNullOrWhiteSpace(tail))
{
var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
var typeMap = virtualTableManager.GetAllVirtualTables().Where(o => o.GetTaleAllTails().Contains(tail)).Select(o => o.EntityType).ToHashSet();
//设置分表
var mutableEntityTypes = modelBuilder.Model.GetEntityTypes().Where(o => o.ClrType.IsShardingTable() && typeMap.Contains(o.ClrType));
foreach (var entityType in mutableEntityTypes)
{
var shardingEntityConfig = ShardingKeyUtil.Parse(entityType.ClrType);
var shardingEntity = shardingEntityConfig.ShardingEntityType;
var tailPrefix = shardingEntityConfig.TailPrefix;
var entity = modelBuilder.Entity(shardingEntity);
var tableName = shardingEntityConfig.ShardingOriginalTable;
if (string.IsNullOrWhiteSpace(tableName))
throw new ArgumentNullException($"{shardingEntity}: not found original table name。");
#if DEBUG
Console.WriteLine($"mapping table :[tableName]-->[{tableName}{tailPrefix}{tail}]");
Console.WriteLine($"mapping table :[tableName]-->[{tableName}{tailPrefix}{tail}]");
#endif
entity.ToTable($"{tableName}{tailPrefix}{tail}");
}
entity.ToTable($"{tableName}{tailPrefix}{tail}");
}
}
}

View File

@ -208,9 +208,9 @@ namespace ShardingCore.Sharding
}
public override DatabaseFacade Database => _dbContextCaches.Any()
? _dbContextCaches.First().Value.Database
: GetDbContext(true, string.Empty).Database;
//public override DatabaseFacade Database => _dbContextCaches.Any()
// ? _dbContextCaches.First().Value.Database
// : GetDbContext(true, string.Empty).Database;
public override EntityEntry<TEntity> Entry<TEntity>(TEntity entity)
{

View File

@ -48,18 +48,27 @@ namespace ShardingCore.Sharding
return Task.Run(async () =>
{
using (var scope = _mergeContext.CreateScope())
try
{
var shardingContext = ShardingContext.Create(routeResult);
scope.ShardingAccessor.ShardingContext = shardingContext;
var shardingDbContext = _mergeContext.CreateDbContext(tail);
_parallelDbContexts.Add(shardingDbContext);
var newQueryable = (IQueryable<T>)_mergeContext.GetReWriteQueryable()
.ReplaceDbContextQueryable(shardingDbContext);
using (var scope = _mergeContext.CreateScope())
{
var shardingContext = ShardingContext.Create(routeResult);
scope.ShardingAccessor.ShardingContext = shardingContext;
var asyncEnumerator = await GetAsyncEnumerator(newQueryable);
return new StreamMergeAsyncEnumerator<T>(asyncEnumerator);
var shardingDbContext = _mergeContext.CreateDbContext(tail);
_parallelDbContexts.Add(shardingDbContext);
var newQueryable = (IQueryable<T>) _mergeContext.GetReWriteQueryable()
.ReplaceDbContextQueryable(shardingDbContext);
var asyncEnumerator = await GetAsyncEnumerator(newQueryable);
return new StreamMergeAsyncEnumerator<T>(asyncEnumerator);
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
});
}).ToArray();

View File

@ -1,5 +1,5 @@
{
"SqlServer": {
"ConnectionString": "Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True"
"ConnectionString": "Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;MultipleActiveResultSets=true"
}
}

View File

@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
@ -47,23 +48,31 @@ namespace ShardingCore.Test50
[Fact]
public async Task ToList_All_Test()
{
var mods = await _virtualDbContext.Set<SysUserMod>().ToListAsync();
Assert.Equal(1000, mods.Count);
var modOrders1 = await _virtualDbContext.Set<SysUserMod>().OrderBy(o=>o.Age).ToListAsync();
int ascAge = 1;
foreach (var sysUserMod in modOrders1)
try
{
Assert.Equal(ascAge, sysUserMod.Age);
ascAge++;
var mods = await _virtualDbContext.Set<SysUserMod>().ToListAsync();
Assert.Equal(1000, mods.Count);
var modOrders1 = await _virtualDbContext.Set<SysUserMod>().OrderBy(o => o.Age).ToListAsync();
int ascAge = 1;
foreach (var sysUserMod in modOrders1)
{
Assert.Equal(ascAge, sysUserMod.Age);
ascAge++;
}
var modOrders2 = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Age).ToListAsync();
int descAge = 1000;
foreach (var sysUserMod in modOrders2)
{
Assert.Equal(descAge, sysUserMod.Age);
descAge--;
}
}
var modOrders2 = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Age).ToListAsync();
int descAge = 1000;
foreach (var sysUserMod in modOrders2)
catch (Exception e)
{
Assert.Equal(descAge, sysUserMod.Age);
descAge--;
throw;
}
}