perfect for building test 1
This commit is contained in:
parent
9a53dcd684
commit
f3aa01e999
|
@ -16,9 +16,9 @@ namespace Sample.SqlServer.Controllers
|
||||||
public class ValuesController : ControllerBase
|
public class ValuesController : ControllerBase
|
||||||
{
|
{
|
||||||
|
|
||||||
private readonly DefaultTableDbContext _defaultTableDbContext;
|
private readonly DefaultShardingDbContext _defaultTableDbContext;
|
||||||
|
|
||||||
public ValuesController(DefaultTableDbContext defaultTableDbContext)
|
public ValuesController(DefaultShardingDbContext defaultTableDbContext)
|
||||||
{
|
{
|
||||||
_defaultTableDbContext = defaultTableDbContext;
|
_defaultTableDbContext = defaultTableDbContext;
|
||||||
}
|
}
|
||||||
|
@ -26,9 +26,8 @@ namespace Sample.SqlServer.Controllers
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IActionResult> Get()
|
public async Task<IActionResult> Get()
|
||||||
{
|
{
|
||||||
var result = await _defaultTableDbContext.Set<SysTest>().AnyAsync();
|
var result = await _defaultTableDbContext.Set<SysUserMod>().OrderBy(o=>o.Age).ToListAsync();
|
||||||
var result1 = await _defaultTableDbContext.Set<SysUserMod>().Where(o=>o.Id=="2"||o.Id=="3").ToShardingListAsync();
|
return Ok(result);
|
||||||
return Ok(result1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -31,23 +31,23 @@ namespace Sample.SqlServer
|
||||||
using (var scope=app.ApplicationServices.CreateScope())
|
using (var scope=app.ApplicationServices.CreateScope())
|
||||||
{
|
{
|
||||||
var virtualDbContext =scope.ServiceProvider.GetService<DefaultTableDbContext>();
|
var virtualDbContext =scope.ServiceProvider.GetService<DefaultTableDbContext>();
|
||||||
if (!virtualDbContext.Set<SysUserMod>().ShardingAny())
|
//if (!virtualDbContext.Set<SysUserMod>().ShardingAny())
|
||||||
{
|
//{
|
||||||
var ids = Enumerable.Range(1, 1000);
|
// var ids = Enumerable.Range(1, 1000);
|
||||||
var userMods = new List<SysUserMod>();
|
// var userMods = new List<SysUserMod>();
|
||||||
foreach (var id in ids)
|
// foreach (var id in ids)
|
||||||
{
|
// {
|
||||||
userMods.Add(new SysUserMod()
|
// userMods.Add(new SysUserMod()
|
||||||
{
|
// {
|
||||||
Id = id.ToString(),
|
// Id = id.ToString(),
|
||||||
Age = id,
|
// Age = id,
|
||||||
Name = $"name_{id}",
|
// Name = $"name_{id}",
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
|
|
||||||
virtualDbContext.AddRange(userMods);
|
// virtualDbContext.AddRange(userMods);
|
||||||
virtualDbContext.SaveChanges();
|
// virtualDbContext.SaveChanges();
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Sample.SqlServer.Domain.Maps;
|
using Sample.SqlServer.Domain.Maps;
|
||||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||||
|
using ShardingCore.Sharding.Abstractions;
|
||||||
|
|
||||||
namespace Sample.SqlServer.DbContexts
|
namespace Sample.SqlServer.DbContexts
|
||||||
{
|
{
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace Sample.SqlServer.Domain.Entities
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户Id用于分表
|
/// 用户Id用于分表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ShardingTableKey(TailPrefix = "")]
|
[ShardingTableKey(TailPrefix = "_")]
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户名称
|
/// 用户名称
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
"dotnetRunMessages": "true",
|
"dotnetRunMessages": "true",
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"launchUrl": "values/get",
|
"launchUrl": "values/get",
|
||||||
"applicationUrl": "https://localhost:5001;http://localhost:5000",
|
"applicationUrl": "http://localhost:5000",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,8 +32,11 @@ namespace Sample.SqlServer
|
||||||
//o.AddDataSourceVirtualRoute<>();
|
//o.AddDataSourceVirtualRoute<>();
|
||||||
|
|
||||||
});
|
});
|
||||||
services.AddDbContext<DefaultTableDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB123;Integrated Security=True")
|
services.AddDbContext<DefaultTableDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True"));
|
||||||
.UseShardingSqlServerUpdateSqlGenerator());
|
|
||||||
|
|
||||||
|
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.
|
// 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.UseRouting();
|
||||||
|
|
||||||
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
||||||
app.DbSeed();
|
//app.DbSeed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -38,6 +38,7 @@ namespace ShardingCore.SqlServer
|
||||||
}
|
}
|
||||||
public DbContextOptions GetDbContextOptions(DbConnection dbConnection)
|
public DbContextOptions GetDbContextOptions(DbConnection dbConnection)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("create new dbcontext options,dbconnection is new:"+(dbConnection==null));
|
||||||
|
|
||||||
var track = dbConnection != null;
|
var track = dbConnection != null;
|
||||||
var connection = dbConnection ?? GetSqlConnection();
|
var connection = dbConnection ?? GetSqlConnection();
|
||||||
|
|
|
@ -37,9 +37,6 @@ namespace ShardingCore
|
||||||
//services.AddSingleton(typeof(IVirtualTable<>), typeof(OneDbVirtualTable<>));
|
//services.AddSingleton(typeof(IVirtualTable<>), typeof(OneDbVirtualTable<>));
|
||||||
services.AddSingleton<IShardingAccessor, ShardingAccessor>();
|
services.AddSingleton<IShardingAccessor, ShardingAccessor>();
|
||||||
services.AddSingleton<IShardingScopeFactory, ShardingScopeFactory>();
|
services.AddSingleton<IShardingScopeFactory, ShardingScopeFactory>();
|
||||||
//分表
|
|
||||||
services.AddSingleton<IShardingTableAccessor, ShardingTableAccessor>();
|
|
||||||
services.AddSingleton<IShardingTableScopeFactory, ShardingTableScopeFactory>();
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,14 +21,12 @@ namespace ShardingCore.DbContexts
|
||||||
public class ShardingDbContextFactory:IShardingDbContextFactory
|
public class ShardingDbContextFactory:IShardingDbContextFactory
|
||||||
{
|
{
|
||||||
private readonly IShardingCoreOptions _shardingCoreOptions;
|
private readonly IShardingCoreOptions _shardingCoreOptions;
|
||||||
private readonly IShardingTableScopeFactory _shardingTableScopeFactory;
|
|
||||||
private readonly IDbContextCreateFilterManager _dbContextCreateFilterManager;
|
private readonly IDbContextCreateFilterManager _dbContextCreateFilterManager;
|
||||||
private readonly IDbContextOptionsProvider _dbContextOptionsProvider;
|
private readonly IDbContextOptionsProvider _dbContextOptionsProvider;
|
||||||
|
|
||||||
public ShardingDbContextFactory(IShardingCoreOptions shardingCoreOptions,IShardingTableScopeFactory shardingTableScopeFactory, IDbContextCreateFilterManager dbContextCreateFilterManager,IDbContextOptionsProvider dbContextOptionsProvider)
|
public ShardingDbContextFactory(IShardingCoreOptions shardingCoreOptions, IDbContextCreateFilterManager dbContextCreateFilterManager,IDbContextOptionsProvider dbContextOptionsProvider)
|
||||||
{
|
{
|
||||||
_shardingCoreOptions = shardingCoreOptions;
|
_shardingCoreOptions = shardingCoreOptions;
|
||||||
_shardingTableScopeFactory = shardingTableScopeFactory;
|
|
||||||
_dbContextCreateFilterManager = dbContextCreateFilterManager;
|
_dbContextCreateFilterManager = dbContextCreateFilterManager;
|
||||||
_dbContextOptionsProvider = dbContextOptionsProvider;
|
_dbContextOptionsProvider = dbContextOptionsProvider;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,34 +29,30 @@ namespace ShardingCore.EFCores
|
||||||
public override void Customize(ModelBuilder modelBuilder, DbContext context)
|
public override void Customize(ModelBuilder modelBuilder, DbContext context)
|
||||||
{
|
{
|
||||||
base.Customize(modelBuilder, context);
|
base.Customize(modelBuilder, context);
|
||||||
if (context is IShardingTableDbContext)
|
if (context is IShardingTableDbContext shardingTableDbContext)
|
||||||
{
|
{
|
||||||
var shardingTableAccessor = ShardingContainer.Services.GetService<IShardingTableAccessor>();
|
var tail = shardingTableDbContext.GetShardingTableDbContextTail();
|
||||||
|
|
||||||
if (shardingTableAccessor.Context != null)
|
if (!string.IsNullOrWhiteSpace(tail))
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrWhiteSpace(shardingTableAccessor.Context.Tail))
|
var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
|
||||||
{
|
var typeMap = virtualTableManager.GetAllVirtualTables().Where(o => o.GetTaleAllTails().Contains(tail)).Select(o => o.EntityType).ToHashSet();
|
||||||
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 mutableEntityTypes = modelBuilder.Model.GetEntityTypes().Where(o => o.ClrType.IsShardingTable()&&typeMap.Contains(o.ClrType));
|
var mutableEntityTypes = modelBuilder.Model.GetEntityTypes().Where(o => o.ClrType.IsShardingTable() && typeMap.Contains(o.ClrType));
|
||||||
foreach (var entityType in mutableEntityTypes)
|
foreach (var entityType in mutableEntityTypes)
|
||||||
{
|
{
|
||||||
var shardingEntityConfig = ShardingKeyUtil.Parse(entityType.ClrType);
|
var shardingEntityConfig = ShardingKeyUtil.Parse(entityType.ClrType);
|
||||||
var shardingEntity = shardingEntityConfig.ShardingEntityType;
|
var shardingEntity = shardingEntityConfig.ShardingEntityType;
|
||||||
var tailPrefix = shardingEntityConfig.TailPrefix;
|
var tailPrefix = shardingEntityConfig.TailPrefix;
|
||||||
var entity = modelBuilder.Entity(shardingEntity);
|
var entity = modelBuilder.Entity(shardingEntity);
|
||||||
var tableName = shardingEntityConfig.ShardingOriginalTable;
|
var tableName = shardingEntityConfig.ShardingOriginalTable;
|
||||||
if (string.IsNullOrWhiteSpace(tableName))
|
if (string.IsNullOrWhiteSpace(tableName))
|
||||||
throw new ArgumentNullException($"{shardingEntity}: not found original table name。");
|
throw new ArgumentNullException($"{shardingEntity}: not found original table name。");
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
Console.WriteLine($"mapping table :[tableName]-->[{tableName}{tailPrefix}{tail}]");
|
Console.WriteLine($"mapping table :[tableName]-->[{tableName}{tailPrefix}{tail}]");
|
||||||
#endif
|
#endif
|
||||||
entity.ToTable($"{tableName}{tailPrefix}{tail}");
|
entity.ToTable($"{tableName}{tailPrefix}{tail}");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -208,9 +208,9 @@ namespace ShardingCore.Sharding
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public override DatabaseFacade Database => _dbContextCaches.Any()
|
//public override DatabaseFacade Database => _dbContextCaches.Any()
|
||||||
? _dbContextCaches.First().Value.Database
|
// ? _dbContextCaches.First().Value.Database
|
||||||
: GetDbContext(true, string.Empty).Database;
|
// : GetDbContext(true, string.Empty).Database;
|
||||||
|
|
||||||
public override EntityEntry<TEntity> Entry<TEntity>(TEntity entity)
|
public override EntityEntry<TEntity> Entry<TEntity>(TEntity entity)
|
||||||
{
|
{
|
||||||
|
|
|
@ -48,18 +48,27 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
return Task.Run(async () =>
|
return Task.Run(async () =>
|
||||||
{
|
{
|
||||||
using (var scope = _mergeContext.CreateScope())
|
try
|
||||||
{
|
{
|
||||||
var shardingContext = ShardingContext.Create(routeResult);
|
|
||||||
scope.ShardingAccessor.ShardingContext = shardingContext;
|
|
||||||
|
|
||||||
var shardingDbContext = _mergeContext.CreateDbContext(tail);
|
using (var scope = _mergeContext.CreateScope())
|
||||||
_parallelDbContexts.Add(shardingDbContext);
|
{
|
||||||
var newQueryable = (IQueryable<T>)_mergeContext.GetReWriteQueryable()
|
var shardingContext = ShardingContext.Create(routeResult);
|
||||||
.ReplaceDbContextQueryable(shardingDbContext);
|
scope.ShardingAccessor.ShardingContext = shardingContext;
|
||||||
|
|
||||||
var asyncEnumerator = await GetAsyncEnumerator(newQueryable);
|
var shardingDbContext = _mergeContext.CreateDbContext(tail);
|
||||||
return new StreamMergeAsyncEnumerator<T>(asyncEnumerator);
|
_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();
|
}).ToArray();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"SqlServer": {
|
"SqlServer": {
|
||||||
"ConnectionString": "Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True"
|
"ConnectionString": "Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;MultipleActiveResultSets=true"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
@ -47,23 +48,31 @@ namespace ShardingCore.Test50
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ToList_All_Test()
|
public async Task ToList_All_Test()
|
||||||
{
|
{
|
||||||
var mods = await _virtualDbContext.Set<SysUserMod>().ToListAsync();
|
try
|
||||||
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 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--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
var modOrders2 = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Age).ToListAsync();
|
|
||||||
int descAge = 1000;
|
|
||||||
foreach (var sysUserMod in modOrders2)
|
|
||||||
{
|
{
|
||||||
Assert.Equal(descAge, sysUserMod.Age);
|
throw;
|
||||||
descAge--;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue