优化部分使用修改部分demo的使用正确性,
This commit is contained in:
parent
b731c41f02
commit
b73127b11a
|
@ -8,9 +8,9 @@
|
||||||
|
|
||||||
Release | EF Core | .NET Standard | .NET (Core) | Sql Server | [Pomelo.EntityFrameworkCore.MySql](https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql)
|
Release | EF Core | .NET Standard | .NET (Core) | Sql Server | [Pomelo.EntityFrameworkCore.MySql](https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql)
|
||||||
--- | --- | --- | --- | --- | ---
|
--- | --- | --- | --- | --- | ---
|
||||||
[5.1.x.x](https://www.nuget.org/packages/ShardingCore/5.1.0.3) | >= 5.0.x | 2.1 | 3.0+ | >= 2012 | 5.0.0-alpha.2
|
[5.1.x.x](https://www.nuget.org/packages/ShardingCore/5.1.0.9) | >= 5.0.x | 2.1 | 3.0+ | >= 2012 | 5.0.0-alpha.2
|
||||||
[3.1.x.x](https://www.nuget.org/packages/ShardingCore/3.1.0.3) | 3.1.10 | 2.0 | 2.0+ | >= 2012 | 3.2.4
|
[3.1.x.x](https://www.nuget.org/packages/ShardingCore/3.1.0.9) | 3.1.10 | 2.0 | 2.0+ | >= 2012 | 3.2.4
|
||||||
[2.1.x.x](https://www.nuget.org/packages/ShardingCore/2.1.0.3) | 2.2.6 | 2.0 | 2.0+ | >= 2008 | 2.2.6
|
[2.1.x.x](https://www.nuget.org/packages/ShardingCore/2.1.0.9) | 2.2.6 | 2.0 | 2.0+ | >= 2008 | 2.2.6
|
||||||
|
|
||||||
|
|
||||||
- [开始](#开始)
|
- [开始](#开始)
|
||||||
|
@ -80,7 +80,7 @@ Release | EF Core | .NET Standard | .NET (Core) | Sql Server | [Pomelo.EntityFr
|
||||||
|
|
||||||
## 安装
|
## 安装
|
||||||
```xml
|
```xml
|
||||||
<PackageReference Include="ShardingCore.SqlServer" Version="5.1.0.3" />
|
<PackageReference Include="ShardingCore.SqlServer" Version="5.1.0.9" />
|
||||||
```
|
```
|
||||||
|
|
||||||
## 配置
|
## 配置
|
||||||
|
|
|
@ -41,9 +41,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.AutoByDate.SqlServe
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.MySql", "samples\Sample.MySql\Sample.MySql.csproj", "{90675788-D5C3-415A-9C18-FF159A75B4D5}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.MySql", "samples\Sample.MySql\Sample.MySql.csproj", "{90675788-D5C3-415A-9C18-FF159A75B4D5}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.SqlServer3x", "samples\Sample.SqlServer3x\Sample.SqlServer3x.csproj", "{447D5357-F095-45DE-9DA5-2D9997237366}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.SqlServer3x", "samples\Sample.SqlServer3x\Sample.SqlServer3x.csproj", "{447D5357-F095-45DE-9DA5-2D9997237366}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCoreTestSqlServer3x", "test\ShardingCoreTestSqlServer3x\ShardingCoreTestSqlServer3x.csproj", "{1CE858B8-56D8-4009-BF46-EE0F79F259D1}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCoreTestSqlServer3x", "test\ShardingCoreTestSqlServer3x\ShardingCoreTestSqlServer3x.csproj", "{1CE858B8-56D8-4009-BF46-EE0F79F259D1}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCoreTestBatch", "test\ShardingCoreTestBatch\ShardingCoreTestBatch.csproj", "{54BA9F11-96CD-47DF-97FB-0BC83D2F7081}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
@ -115,6 +117,10 @@ Global
|
||||||
{1CE858B8-56D8-4009-BF46-EE0F79F259D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{1CE858B8-56D8-4009-BF46-EE0F79F259D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{1CE858B8-56D8-4009-BF46-EE0F79F259D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{1CE858B8-56D8-4009-BF46-EE0F79F259D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{1CE858B8-56D8-4009-BF46-EE0F79F259D1}.Release|Any CPU.Build.0 = Release|Any CPU
|
{1CE858B8-56D8-4009-BF46-EE0F79F259D1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{54BA9F11-96CD-47DF-97FB-0BC83D2F7081}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{54BA9F11-96CD-47DF-97FB-0BC83D2F7081}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{54BA9F11-96CD-47DF-97FB-0BC83D2F7081}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{54BA9F11-96CD-47DF-97FB-0BC83D2F7081}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -136,6 +142,7 @@ Global
|
||||||
{90675788-D5C3-415A-9C18-FF159A75B4D5} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
{90675788-D5C3-415A-9C18-FF159A75B4D5} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
||||||
{447D5357-F095-45DE-9DA5-2D9997237366} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
{447D5357-F095-45DE-9DA5-2D9997237366} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
||||||
{1CE858B8-56D8-4009-BF46-EE0F79F259D1} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
|
{1CE858B8-56D8-4009-BF46-EE0F79F259D1} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
|
||||||
|
{54BA9F11-96CD-47DF-97FB-0BC83D2F7081} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {8C07A667-E8B4-43C7-8053-721584BAD291}
|
SolutionGuid = {8C07A667-E8B4-43C7-8053-721584BAD291}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
:start
|
:start
|
||||||
::定义版本
|
::定义版本
|
||||||
set EFCORE2=2.1.0.8
|
set EFCORE2=2.1.0.10
|
||||||
set EFCORE3=3.1.0.8
|
set EFCORE3=3.1.0.10
|
||||||
set EFCORE5=5.1.0.8
|
set EFCORE5=5.1.0.10
|
||||||
|
|
||||||
::删除所有bin与obj下的文件
|
::删除所有bin与obj下的文件
|
||||||
@echo off
|
@echo off
|
||||||
|
|
|
@ -6,7 +6,7 @@ namespace Sample.MySql.DbContexts
|
||||||
{
|
{
|
||||||
public class DefaultTableDbContext: DbContext,IShardingTableDbContext
|
public class DefaultTableDbContext: DbContext,IShardingTableDbContext
|
||||||
{
|
{
|
||||||
public DefaultTableDbContext(ShardingDbContextOptions shardingDbContextOptions):base(shardingDbContextOptions.DbContextOptions)
|
public DefaultTableDbContext(DbContextOptions<DefaultTableDbContext> options) :base(options)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ namespace Sample.SqlServer.DbContexts
|
||||||
{
|
{
|
||||||
public class DefaultTableDbContext: DbContext,IShardingTableDbContext
|
public class DefaultTableDbContext: DbContext,IShardingTableDbContext
|
||||||
{
|
{
|
||||||
public DefaultTableDbContext(ShardingDbContextOptions shardingDbContextOptions):base(shardingDbContextOptions.DbContextOptions)
|
public DefaultTableDbContext(DbContextOptions<DefaultTableDbContext> options) :base(options)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,12 +8,12 @@ using ShardingCore.TableCreator;
|
||||||
|
|
||||||
namespace Samples.AutoByDate.SqlServer.Jobs
|
namespace Samples.AutoByDate.SqlServer.Jobs
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* @Author: xjm
|
* @Author: xjm
|
||||||
* @Description:
|
* @Description:
|
||||||
* @Date: Tuesday, 02 February 2021 17:24:17
|
* @Date: Tuesday, 02 February 2021 17:24:17
|
||||||
* @Email: 326308290@qq.com
|
* @Email: 326308290@qq.com
|
||||||
*/
|
*/
|
||||||
//public class AutoCreateTableByDay : IJob
|
//public class AutoCreateTableByDay : IJob
|
||||||
//{
|
//{
|
||||||
// /// <summary>
|
// /// <summary>
|
||||||
|
@ -34,8 +34,8 @@ namespace Samples.AutoByDate.SqlServer.Jobs
|
||||||
// var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
// var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||||
// try
|
// try
|
||||||
// {
|
// {
|
||||||
// tableCreator.CreateTable<SysUserLogByDay>(tail);
|
|
||||||
// virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
// virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||||
|
// tableCreator.CreateTable<SysUserLogByDay>(tail);
|
||||||
// }
|
// }
|
||||||
// catch (Exception e)
|
// catch (Exception e)
|
||||||
// {
|
// {
|
||||||
|
|
|
@ -35,7 +35,7 @@ namespace ShardingCore.MySql
|
||||||
{
|
{
|
||||||
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig(connectKey);
|
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig(connectKey);
|
||||||
var shardingDbContextOptions = new ShardingDbContextOptions(CreateOptions(connectKey,shardingConfigEntry.ConnectionString), tail);
|
var shardingDbContextOptions = new ShardingDbContextOptions(CreateOptions(connectKey,shardingConfigEntry.ConnectionString), tail);
|
||||||
return _shardingDbContextFactory.Create(connectKey, shardingDbContextOptions,null);
|
return _shardingDbContextFactory.Create(connectKey, shardingDbContextOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DbContextOptions CreateOptions(string connectKey, string connectionString)
|
private DbContextOptions CreateOptions(string connectKey, string connectionString)
|
||||||
|
|
|
@ -36,7 +36,7 @@ namespace ShardingCore.SqlServer
|
||||||
{
|
{
|
||||||
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig(connectKey);
|
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig(connectKey);
|
||||||
var shardingDbContextOptions = new ShardingDbContextOptions(CreateOptions(connectKey, shardingConfigEntry.ConnectionString), tail);
|
var shardingDbContextOptions = new ShardingDbContextOptions(CreateOptions(connectKey, shardingConfigEntry.ConnectionString), tail);
|
||||||
return _shardingDbContextFactory.Create(connectKey, shardingDbContextOptions,null);
|
return _shardingDbContextFactory.Create(connectKey, shardingDbContextOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DbContextOptions CreateOptions(string connectKey, string connectString)
|
private DbContextOptions CreateOptions(string connectKey, string connectString)
|
||||||
|
|
|
@ -19,6 +19,6 @@ namespace ShardingCore.DbContexts.Abstractions
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dbContext"></param>
|
/// <param name="dbContext"></param>
|
||||||
/// <param name="s"></param>
|
/// <param name="s"></param>
|
||||||
void CreateAfter(DbContext dbContext, IServiceProvider s);
|
void CreateAfter(DbContext dbContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ namespace ShardingCore.DbContexts
|
||||||
*/
|
*/
|
||||||
public interface IShardingDbContextFactory
|
public interface IShardingDbContextFactory
|
||||||
{
|
{
|
||||||
DbContext Create(string connectKey,ShardingDbContextOptions shardingDbContextOptions,IServiceProvider serviceProvider);
|
DbContext Create(string connectKey,ShardingDbContextOptions shardingDbContextOptions);
|
||||||
DbContext Create(string connectKey,string tail, IServiceProvider serviceProvider);
|
DbContext Create(string connectKey,string tail, IDbContextOptionsProvider dbContextOptionsProvider);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -28,7 +28,7 @@ namespace ShardingCore.DbContexts
|
||||||
_shardingTableScopeFactory = shardingTableScopeFactory;
|
_shardingTableScopeFactory = shardingTableScopeFactory;
|
||||||
_dbContextCreateFilterManager = dbContextCreateFilterManager;
|
_dbContextCreateFilterManager = dbContextCreateFilterManager;
|
||||||
}
|
}
|
||||||
public DbContext Create(string connectKey, ShardingDbContextOptions shardingDbContextOptions,IServiceProvider serviceProvider)
|
public DbContext Create(string connectKey, ShardingDbContextOptions shardingDbContextOptions)
|
||||||
{
|
{
|
||||||
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig(connectKey);
|
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig(connectKey);
|
||||||
|
|
||||||
|
@ -48,15 +48,12 @@ namespace ShardingCore.DbContexts
|
||||||
shardingTableDbContext.ModelChangeKey = modelChangeKey;
|
shardingTableDbContext.ModelChangeKey = modelChangeKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (serviceProvider != null)
|
var filters = _dbContextCreateFilterManager.GetFilters();
|
||||||
|
if (filters.Any())
|
||||||
{
|
{
|
||||||
var filters = _dbContextCreateFilterManager.GetFilters();
|
foreach (var dbContextCreateFilter in filters)
|
||||||
if (filters.Any())
|
|
||||||
{
|
{
|
||||||
foreach (var dbContextCreateFilter in filters)
|
dbContextCreateFilter.CreateAfter(dbContext);
|
||||||
{
|
|
||||||
dbContextCreateFilter.CreateAfter(dbContext, serviceProvider);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var dbContextModel = dbContext.Model;
|
var dbContextModel = dbContext.Model;
|
||||||
|
@ -64,12 +61,11 @@ namespace ShardingCore.DbContexts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbContext Create(string connectKey, string tail,IServiceProvider serviceProvider)
|
public DbContext Create(string connectKey, string tail, IDbContextOptionsProvider dbContextOptionsProvider)
|
||||||
{
|
{
|
||||||
var dbContextOptionsProvider = serviceProvider.GetService<IDbContextOptionsProvider>();
|
|
||||||
var shardingDbContextOptions =
|
var shardingDbContextOptions =
|
||||||
new ShardingDbContextOptions(dbContextOptionsProvider.GetDbContextOptions(connectKey), tail);
|
new ShardingDbContextOptions(dbContextOptionsProvider.GetDbContextOptions(connectKey), tail);
|
||||||
return Create(connectKey,shardingDbContextOptions,serviceProvider);
|
return Create(connectKey,shardingDbContextOptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,6 +5,7 @@ using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using ShardingCore.Core;
|
using ShardingCore.Core;
|
||||||
using ShardingCore.Core.VirtualDataSources;
|
using ShardingCore.Core.VirtualDataSources;
|
||||||
using ShardingCore.Core.VirtualRoutes;
|
using ShardingCore.Core.VirtualRoutes;
|
||||||
|
@ -382,7 +383,7 @@ namespace ShardingCore.DbContexts.VirtualDbContexts
|
||||||
}
|
}
|
||||||
if(!dbContexts.TryGetValue(tail,out var dbContext))
|
if(!dbContexts.TryGetValue(tail,out var dbContext))
|
||||||
{
|
{
|
||||||
dbContext = _shardingDbContextFactory.Create(connectKey, tail == EMPTY_SHARDING_TAIL_ID ? string.Empty : tail, _serviceProvider);
|
dbContext = _shardingDbContextFactory.Create(connectKey, tail == EMPTY_SHARDING_TAIL_ID ? string.Empty : tail, _serviceProvider.GetService<IDbContextOptionsProvider>());
|
||||||
dbContexts.TryAdd(tail, dbContext);
|
dbContexts.TryAdd(tail, dbContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,7 @@ namespace ShardingCore
|
||||||
var dbContextOptionsProvider = scope.ServiceProvider.GetService<IDbContextOptionsProvider>();
|
var dbContextOptionsProvider = scope.ServiceProvider.GetService<IDbContextOptionsProvider>();
|
||||||
using var context = _shardingDbContextFactory.Create(connectKey,
|
using var context = _shardingDbContextFactory.Create(connectKey,
|
||||||
new ShardingDbContextOptions(dbContextOptionsProvider.GetDbContextOptions(connectKey),
|
new ShardingDbContextOptions(dbContextOptionsProvider.GetDbContextOptions(connectKey),
|
||||||
string.Empty),scope.ServiceProvider);
|
string.Empty));
|
||||||
foreach (var entity in context.Model.GetEntityTypes())
|
foreach (var entity in context.Model.GetEntityTypes())
|
||||||
{
|
{
|
||||||
_virtualDataSourceManager.AddConnectEntities(connectKey, entity.ClrType);
|
_virtualDataSourceManager.AddConnectEntities(connectKey, entity.ClrType);
|
||||||
|
@ -186,7 +186,7 @@ namespace ShardingCore
|
||||||
var dbContextOptionsProvider = scope.ServiceProvider.GetService<IDbContextOptionsProvider>();
|
var dbContextOptionsProvider = scope.ServiceProvider.GetService<IDbContextOptionsProvider>();
|
||||||
using var context = _shardingDbContextFactory.Create(shardingConfig.ConnectKey,
|
using var context = _shardingDbContextFactory.Create(shardingConfig.ConnectKey,
|
||||||
new ShardingDbContextOptions(
|
new ShardingDbContextOptions(
|
||||||
dbContextOptionsProvider.GetDbContextOptions(shardingConfig.ConnectKey), string.Empty),scope.ServiceProvider);
|
dbContextOptionsProvider.GetDbContextOptions(shardingConfig.ConnectKey), string.Empty));
|
||||||
var modelCacheSyncObject = context.GetModelCacheSyncObject();
|
var modelCacheSyncObject = context.GetModelCacheSyncObject();
|
||||||
|
|
||||||
lock (modelCacheSyncObject)
|
lock (modelCacheSyncObject)
|
||||||
|
|
|
@ -56,7 +56,7 @@ namespace ShardingCore.TableCreator
|
||||||
var dbContextOptionsProvider = serviceScope.ServiceProvider.GetService<IDbContextOptionsProvider>();
|
var dbContextOptionsProvider = serviceScope.ServiceProvider.GetService<IDbContextOptionsProvider>();
|
||||||
var virtualTable = _virtualTableManager.GetVirtualTable(connectKey,shardingEntityType);
|
var virtualTable = _virtualTableManager.GetVirtualTable(connectKey,shardingEntityType);
|
||||||
|
|
||||||
using (var dbContext = _shardingDbContextFactory.Create(connectKey,new ShardingDbContextOptions(dbContextOptionsProvider.GetDbContextOptions(connectKey), tail),serviceScope.ServiceProvider))
|
using (var dbContext = _shardingDbContextFactory.Create(connectKey,new ShardingDbContextOptions(dbContextOptionsProvider.GetDbContextOptions(connectKey), tail)))
|
||||||
{
|
{
|
||||||
var modelCacheSyncObject = dbContext.GetModelCacheSyncObject();
|
var modelCacheSyncObject = dbContext.GetModelCacheSyncObject();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||||
|
using ShardingCoreTestBatch.Domain.Maps;
|
||||||
|
|
||||||
|
namespace ShardingCoreTestBatch
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* @Author: xjm
|
||||||
|
* @Description:
|
||||||
|
* @Date: 2021/3/31 15:28:11
|
||||||
|
* @Ver: 1.0
|
||||||
|
* @Email: 326308290@qq.com
|
||||||
|
*/
|
||||||
|
public class DefaultDbContext : DbContext, IShardingTableDbContext
|
||||||
|
{
|
||||||
|
public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
base.OnModelCreating(modelBuilder);
|
||||||
|
modelBuilder.ApplyConfiguration(new SysUserModMap());
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ModelChangeKey { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
using ShardingCore.Core;
|
||||||
|
|
||||||
|
namespace ShardingCoreTestBatch.Domain.Entities
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* @Author: xjm
|
||||||
|
* @Description:
|
||||||
|
* @Date: Thursday, 14 January 2021 15:36:43
|
||||||
|
* @Email: 326308290@qq.com
|
||||||
|
*/
|
||||||
|
public class SysUserMod:IShardingTable
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 用户Id用于分表
|
||||||
|
/// </summary>
|
||||||
|
[ShardingTableKey(TailPrefix = "_")]
|
||||||
|
public string Id { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 用户名称
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 用户姓名
|
||||||
|
/// </summary>
|
||||||
|
public int Age { get; set; }
|
||||||
|
public int AgeGroup { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||||
|
using ShardingCoreTestBatch.Domain.Entities;
|
||||||
|
|
||||||
|
namespace ShardingCoreTestBatch.Domain.Maps
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* @Author: xjm
|
||||||
|
* @Description:
|
||||||
|
* @Date: Thursday, 14 January 2021 15:37:33
|
||||||
|
* @Email: 326308290@qq.com
|
||||||
|
*/
|
||||||
|
public class SysUserModMap:IEntityTypeConfiguration<SysUserMod>
|
||||||
|
{
|
||||||
|
public void Configure(EntityTypeBuilder<SysUserMod> builder)
|
||||||
|
{
|
||||||
|
builder.HasKey(o => o.Id);
|
||||||
|
builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
|
||||||
|
builder.Property(o => o.Name).HasMaxLength(128);
|
||||||
|
builder.ToTable(nameof(SysUserMod));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
|
<LangVersion>9.0</LangVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Remove="Configs\**" />
|
||||||
|
<EmbeddedResource Remove="Configs\**" />
|
||||||
|
<None Remove="Configs\**" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="5.0.2" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
|
<PackageReference Include="Xunit.DependencyInjection" Version="7.1.0" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="5.1.29" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\src\ShardingCore.SqlServer\ShardingCore.SqlServer.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -0,0 +1,58 @@
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
|
||||||
|
using ShardingCore.DbContexts.VirtualDbContexts;
|
||||||
|
using ShardingCore.Extensions;
|
||||||
|
using ShardingCoreTestBatch.Domain.Entities;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace ShardingCoreTestBatch
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* @Author: xjm
|
||||||
|
* @Description:
|
||||||
|
* @Date: Friday, 15 January 2021 17:22:10
|
||||||
|
* @Email: 326308290@qq.com
|
||||||
|
*/
|
||||||
|
public class ShardingTest
|
||||||
|
{
|
||||||
|
private readonly IVirtualDbContext _virtualDbContext;
|
||||||
|
private readonly IRoutingRuleEngineFactory _routingRuleEngineFactory;
|
||||||
|
|
||||||
|
public ShardingTest(IVirtualDbContext virtualDbContext,IRoutingRuleEngineFactory routingRuleEngineFactory)
|
||||||
|
{
|
||||||
|
_virtualDbContext = virtualDbContext;
|
||||||
|
_routingRuleEngineFactory = routingRuleEngineFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Fact]
|
||||||
|
//public async Task Route_TEST()
|
||||||
|
//{
|
||||||
|
// var queryable1 = _virtualDbContext.Set<SysUserMod>().Where(o=>o.Id=="339");
|
||||||
|
// var routeResults1 = _routingRuleEngineFactory.Route(queryable1);
|
||||||
|
// Assert.Equal(1,routeResults1.Count());
|
||||||
|
// Assert.Equal(1,routeResults1.FirstOrDefault().ReplaceTables.Count());
|
||||||
|
// Assert.Equal("0",routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().Tail);
|
||||||
|
// Assert.Equal(nameof(SysUserMod),routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().OriginalName);
|
||||||
|
// var ids = new[] {"339", "124","142"};
|
||||||
|
// var queryable2= _virtualDbContext.Set<SysUserMod>().Where(o=>ids.Contains(o.Id));
|
||||||
|
// var routeResult2s = _routingRuleEngineFactory.Route(queryable2);
|
||||||
|
// Assert.Equal(2,routeResult2s.Count());
|
||||||
|
// Assert.Equal(1,routeResult2s.FirstOrDefault().ReplaceTables.Count());
|
||||||
|
// Assert.Equal(2,routeResult2s.SelectMany(o=>o.ReplaceTables).Count());
|
||||||
|
// Assert.Equal(true,routeResult2s.SelectMany(o=>o.ReplaceTables).All(o=>new[]{"0","1"}.Contains(o.Tail)));
|
||||||
|
//}
|
||||||
|
[Fact]
|
||||||
|
public async Task ToList_All_Test()
|
||||||
|
{
|
||||||
|
var startNew = Stopwatch.StartNew(); startNew.Start();
|
||||||
|
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o=>o.Age== 7601935).ToShardingListAsync();
|
||||||
|
startNew.Stop();
|
||||||
|
var x = startNew.ElapsedMilliseconds;
|
||||||
|
Console.WriteLine(mods.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
using ShardingCore.VirtualRoutes.Mods;
|
||||||
|
using ShardingCoreTestBatch.Domain.Entities;
|
||||||
|
|
||||||
|
namespace ShardingCoreTestBatch.Shardings
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* @Author: xjm
|
||||||
|
* @Description:
|
||||||
|
* @Date: Thursday, 14 January 2021 15:39:27
|
||||||
|
* @Email: 326308290@qq.com
|
||||||
|
*/
|
||||||
|
public class SysUserModVirtualTableRoute : AbstractSimpleShardingModKeyStringVirtualTableRoute<SysUserMod>
|
||||||
|
{
|
||||||
|
public SysUserModVirtualTableRoute() : base(2,15)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,120 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using ShardingCore;
|
||||||
|
using ShardingCore.DbContexts.VirtualDbContexts;
|
||||||
|
using ShardingCore.Extensions;
|
||||||
|
using ShardingCore.SqlServer;
|
||||||
|
using ShardingCoreTestBatch.Domain.Entities;
|
||||||
|
using ShardingCoreTestBatch.Shardings;
|
||||||
|
|
||||||
|
#if EFCORE5SQLSERVER
|
||||||
|
using ShardingCore.SqlServer;
|
||||||
|
#endif
|
||||||
|
#if EFCORE5MYSQL
|
||||||
|
using ShardingCore.MySql;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace ShardingCoreTestBatch
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* @Author: xjm
|
||||||
|
* @Description:
|
||||||
|
* @Date: Friday, 15 January 2021 15:37:46
|
||||||
|
* @Email: 326308290@qq.com
|
||||||
|
*/
|
||||||
|
public class Startup
|
||||||
|
{
|
||||||
|
// // 自定义 host 构建
|
||||||
|
//public void ConfigureHost(IHostBuilder hostBuilder)
|
||||||
|
//{
|
||||||
|
// hostBuilder
|
||||||
|
// .ConfigureAppConfiguration(builder =>
|
||||||
|
// {
|
||||||
|
// builder.AddJsonFile("Configs/DbConfig.json");
|
||||||
|
// });
|
||||||
|
//}
|
||||||
|
|
||||||
|
// 支持的形式:
|
||||||
|
// ConfigureServices(IServiceCollection services)
|
||||||
|
// ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
|
||||||
|
// ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services)
|
||||||
|
public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
|
||||||
|
{
|
||||||
|
|
||||||
|
services.AddShardingSqlServer(o =>
|
||||||
|
{
|
||||||
|
o.EnsureCreatedWithOutShardingTable = true;
|
||||||
|
o.CreateShardingTableOnStart = true;
|
||||||
|
o.AddShardingDbContextWithShardingTable<DefaultDbContext>("conn1", "Data Source=localhost;Initial Catalog=ShardingCoreDBBatch;Integrated Security=True", dbConfig =>
|
||||||
|
{
|
||||||
|
dbConfig.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
||||||
|
});
|
||||||
|
//o.AddDataSourceVirtualRoute<>();
|
||||||
|
|
||||||
|
});
|
||||||
|
//services.AddShardingSqlServer(o =>
|
||||||
|
//{
|
||||||
|
// o.ConnectionString = hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"];
|
||||||
|
// o.AddSharding<SysUserModVirtualTableRoute>();
|
||||||
|
// o.AddSharding<SysUserSalaryVirtualTableRoute>();
|
||||||
|
// o.UseShardingCoreConfig((provider, config) =>
|
||||||
|
// {
|
||||||
|
// config.EnsureCreated = true;
|
||||||
|
// config.CreateShardingTableOnStart = true;
|
||||||
|
// });
|
||||||
|
//});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法
|
||||||
|
public void Configure(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
var shardingBootstrapper = serviceProvider.GetService<IShardingBootstrapper>();
|
||||||
|
shardingBootstrapper.Start();
|
||||||
|
// 有一些测试数据要初始化可以放在这里
|
||||||
|
InitData(serviceProvider).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 添加种子数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="serviceProvider"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private async Task InitData(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
using (var scope = serviceProvider.CreateScope())
|
||||||
|
{
|
||||||
|
var virtualDbContext = scope.ServiceProvider.GetService<IVirtualDbContext>();
|
||||||
|
if (!await virtualDbContext.Set<SysUserMod>().ShardingAnyAsync(o => true))
|
||||||
|
{
|
||||||
|
var ids = Enumerable.Range(1, 9000000);
|
||||||
|
var userMods = new List<SysUserMod>();
|
||||||
|
foreach (var id in ids)
|
||||||
|
{
|
||||||
|
userMods.Add(new SysUserMod()
|
||||||
|
{
|
||||||
|
Id = id.ToString(),
|
||||||
|
Age = id,
|
||||||
|
Name = $"name_{id}",
|
||||||
|
AgeGroup=Math.Abs(id%10)
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
var shardingBatchInsertEntry = virtualDbContext.BulkInsert(userMods);
|
||||||
|
shardingBatchInsertEntry.BatchGroups.ForEach(g =>
|
||||||
|
{
|
||||||
|
g.Key.BulkInsert(g.Value);
|
||||||
|
});
|
||||||
|
|
||||||
|
await virtualDbContext.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue