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 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);
} }
} }
} }

View File

@ -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();
} //}
} }
} }
} }

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 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
{ {

View File

@ -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>
/// 用户名称 /// 用户名称

View File

@ -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"
} }

View File

@ -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();
} }
} }
} }

View File

@ -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();

View File

@ -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;
} }
} }

View File

@ -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;
} }

View File

@ -29,16 +29,13 @@ 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 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 typeMap = virtualTableManager.GetAllVirtualTables().Where(o => o.GetTaleAllTails().Contains(tail)).Select(o => o.EntityType).ToHashSet();
//设置分表 //设置分表
@ -62,4 +59,3 @@ namespace ShardingCore.EFCores
} }
} }
} }
}

View File

@ -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)
{ {

View File

@ -48,6 +48,9 @@ namespace ShardingCore.Sharding
return Task.Run(async () => return Task.Run(async () =>
{ {
try
{
using (var scope = _mergeContext.CreateScope()) using (var scope = _mergeContext.CreateScope())
{ {
var shardingContext = ShardingContext.Create(routeResult); var shardingContext = ShardingContext.Create(routeResult);
@ -61,6 +64,12 @@ namespace ShardingCore.Sharding
var asyncEnumerator = await GetAsyncEnumerator(newQueryable); var asyncEnumerator = await GetAsyncEnumerator(newQueryable);
return new StreamMergeAsyncEnumerator<T>(asyncEnumerator); return new StreamMergeAsyncEnumerator<T>(asyncEnumerator);
} }
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}); });
}).ToArray(); }).ToArray();

View File

@ -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"
} }
} }

View File

@ -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,6 +48,9 @@ namespace ShardingCore.Test50
[Fact] [Fact]
public async Task ToList_All_Test() public async Task ToList_All_Test()
{ {
try
{
var mods = await _virtualDbContext.Set<SysUserMod>().ToListAsync(); var mods = await _virtualDbContext.Set<SysUserMod>().ToListAsync();
Assert.Equal(1000, mods.Count); Assert.Equal(1000, mods.Count);
@ -66,6 +70,11 @@ namespace ShardingCore.Test50
descAge--; descAge--;
} }
} }
catch (Exception e)
{
throw;
}
}
//[Fact] //[Fact]
//public async Task ToList_Join_Test() //public async Task ToList_Join_Test()