recover efcore2.x[#13]
This commit is contained in:
parent
5814464e76
commit
8f665394ff
|
@ -11,6 +11,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src3x", "src3x", "{62AAE0FE
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.3x", "src3x\ShardingCore.3x\ShardingCore.3x.csproj", "{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src2x", "src2x", "{679E6084-0C45-4807-BFEE-D2FDA44B2188}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.2x", "src2x\ShardingCore.2x\ShardingCore.2x.csproj", "{FB92A4BE-A43E-4755-8132-EC38E9650B80}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{CC2C88C0-65F2-445D-BE78-973B840FE281}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50", "test\ShardingCore.Test50\ShardingCore.Test50.csproj", "{7EE133B6-5A02-41B7-9D89-41D9EA14184E}"
|
||||
|
@ -31,7 +35,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.AbpSharding", "samp
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50_3x", "test\ShardingCore.Test50_3x\ShardingCore.Test50_3x.csproj", "{C0A59BB0-F0B8-4AC6-B192-0249E784FC88}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.BulkConsole", "samples\Sample.BulkConsole\Sample.BulkConsole.csproj", "{2443CC8B-FB7D-47A7-9663-F3848BB30A36}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50_2x", "test\ShardingCore.Test50_2x\ShardingCore.Test50_2x.csproj", "{6709DD6A-6A44-4A49-BA4D-A50A40102C8B}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.BulkConsole", "samples\Sample.BulkConsole\Sample.BulkConsole.csproj", "{2443CC8B-FB7D-47A7-9663-F3848BB30A36}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
@ -47,6 +53,10 @@ Global
|
|||
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{FB92A4BE-A43E-4755-8132-EC38E9650B80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{FB92A4BE-A43E-4755-8132-EC38E9650B80}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{FB92A4BE-A43E-4755-8132-EC38E9650B80}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{FB92A4BE-A43E-4755-8132-EC38E9650B80}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{7EE133B6-5A02-41B7-9D89-41D9EA14184E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7EE133B6-5A02-41B7-9D89-41D9EA14184E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7EE133B6-5A02-41B7-9D89-41D9EA14184E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
|
@ -79,6 +89,10 @@ Global
|
|||
{C0A59BB0-F0B8-4AC6-B192-0249E784FC88}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C0A59BB0-F0B8-4AC6-B192-0249E784FC88}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C0A59BB0-F0B8-4AC6-B192-0249E784FC88}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{6709DD6A-6A44-4A49-BA4D-A50A40102C8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6709DD6A-6A44-4A49-BA4D-A50A40102C8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6709DD6A-6A44-4A49-BA4D-A50A40102C8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6709DD6A-6A44-4A49-BA4D-A50A40102C8B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2443CC8B-FB7D-47A7-9663-F3848BB30A36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2443CC8B-FB7D-47A7-9663-F3848BB30A36}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2443CC8B-FB7D-47A7-9663-F3848BB30A36}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
|
@ -90,6 +104,7 @@ Global
|
|||
GlobalSection(NestedProjects) = preSolution
|
||||
{3CAF09A6-6ABD-41D9-BA57-9A822B8095F7} = {490FAE47-4476-4508-B216-505FC850447F}
|
||||
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414} = {62AAE0FE-4099-4A48-AA3C-F76F14C62655}
|
||||
{FB92A4BE-A43E-4755-8132-EC38E9650B80} = {679E6084-0C45-4807-BFEE-D2FDA44B2188}
|
||||
{7EE133B6-5A02-41B7-9D89-41D9EA14184E} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
|
||||
{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
||||
{C8FAB96F-F13E-4094-883C-2D38D39EE4A3} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
|
||||
|
@ -98,6 +113,7 @@ Global
|
|||
{447D5357-F095-45DE-9DA5-2D9997237366} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
||||
{1136B8C9-3539-42FA-97FD-CAA6F146FCF0} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
||||
{C0A59BB0-F0B8-4AC6-B192-0249E784FC88} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
|
||||
{6709DD6A-6A44-4A49-BA4D-A50A40102C8B} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
|
||||
{2443CC8B-FB7D-47A7-9663-F3848BB30A36} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
:start
|
||||
::定义版本
|
||||
set EFCORE2=2.2.0.20
|
||||
set EFCORE3=3.2.0.20
|
||||
set EFCORE5=5.2.0.20
|
||||
|
||||
|
|
|
@ -74,7 +74,6 @@ namespace Sample.BulkConsole
|
|||
|
||||
Console.WriteLine("ok");
|
||||
}
|
||||
|
||||
var b = DateTime.Now.Date.AddDays(-3);
|
||||
var queryable = myShardingDbContext.Set<Order>().Where(o => o.CreateTime >= b).OrderBy(o => o.CreateTime);
|
||||
var startNew1 = Stopwatch.StartNew();
|
||||
|
|
|
@ -74,7 +74,6 @@ namespace Sample.SqlServer.Controllers
|
|||
var sysUserMod98 = result.FirstOrDefault(o => o.Id == "98");
|
||||
_defaultTableDbContext.Attach(sysUserMod98);
|
||||
sysUserMod98.Name = "name_update" + new Random().Next(1, 99) + "_98";
|
||||
_defaultTableDbContext.Attach(sysUserMod98);
|
||||
await _defaultTableDbContext.SaveChangesAsync();
|
||||
var stu = new STU() { Id = "198" };
|
||||
var sresultx111x = _defaultTableDbContext.Set<SysUserMod>().FirstOrDefault(o => o.Id == stu.Id);
|
||||
|
|
|
@ -42,10 +42,26 @@ namespace ShardingCore.Core.PhysicTables
|
|||
return Equals((DefaultPhysicTable) obj);
|
||||
}
|
||||
|
||||
#if !EFCORE2
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(OriginalName, Tail, VirtualTable);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if EFCORE2
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
var hashCode = (OriginalName != null ? OriginalName.GetHashCode() : 0);
|
||||
hashCode = (hashCode * 397) ^ (Tail != null ? Tail.GetHashCode() : 0);
|
||||
hashCode = (hashCode * 397) ^ (VirtualTable != null ? VirtualTable.GetHashCode() : 0);
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
|
@ -17,9 +17,8 @@ namespace ShardingCore.Core.QueryRouteManagers
|
|||
{
|
||||
private static AsyncLocal<ShardingRouteContext> _shardingRouteContext = new AsyncLocal<ShardingRouteContext>();
|
||||
|
||||
/// <summary>
|
||||
/// sharding route context use in using code block
|
||||
/// </summary>
|
||||
|
||||
/// <inheritdoc />
|
||||
public ShardingRouteContext ShardingRouteContext
|
||||
{
|
||||
get => _shardingRouteContext.Value;
|
||||
|
|
|
@ -55,8 +55,13 @@ namespace ShardingCore
|
|||
Action<DbContextOptionsBuilder> shardingOptionAction = option =>
|
||||
{
|
||||
optionsAction?.Invoke(option);
|
||||
#if !EFCORE2
|
||||
option.UseSharding();
|
||||
|
||||
#endif
|
||||
#if EFCORE2
|
||||
option.UseSharding<TShardingDbContext>();
|
||||
#endif
|
||||
};
|
||||
services.AddDbContext<TShardingDbContext>(shardingOptionAction, contextLifetime, optionsLifetime);
|
||||
services.AddInternalShardingCore();
|
||||
|
@ -90,7 +95,13 @@ namespace ShardingCore
|
|||
Action<IServiceProvider, DbContextOptionsBuilder> shardingOptionAction = (sp, option) =>
|
||||
{
|
||||
optionsAction?.Invoke(sp,option);
|
||||
#if !EFCORE2
|
||||
option.UseSharding();
|
||||
|
||||
#endif
|
||||
#if EFCORE2
|
||||
option.UseSharding<TShardingDbContext>();
|
||||
#endif
|
||||
};
|
||||
services.AddDbContext<TShardingDbContext>(shardingOptionAction, contextLifetime, optionsLifetime);
|
||||
services.AddInternalShardingCore();
|
||||
|
@ -168,6 +179,7 @@ namespace ShardingCore
|
|||
services.AddSingleton<IShardingBootstrapper, ShardingBootstrapper>();
|
||||
return services;
|
||||
}
|
||||
#if !EFCORE2
|
||||
internal static DbContextOptionsBuilder UseSharding(this DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
return optionsBuilder.ReplaceService<IDbSetSource, ShardingDbSetSource>()
|
||||
|
@ -175,6 +187,16 @@ namespace ShardingCore
|
|||
.ReplaceService<IRelationalTransactionFactory, ShardingRelationalTransactionFactory>();
|
||||
}
|
||||
|
||||
#endif
|
||||
#if EFCORE2
|
||||
internal static DbContextOptionsBuilder UseSharding<TShardingDbContext>(this DbContextOptionsBuilder optionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
return optionsBuilder.ReplaceService<IDbSetSource, ShardingDbSetSource>()
|
||||
.ReplaceService<IQueryCompiler, ShardingQueryCompiler>()
|
||||
.ReplaceService<IRelationalTransactionFactory, ShardingRelationalTransactionFactory<TShardingDbContext>>();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
internal static DbContextOptionsBuilder UseInnerDbContextSharding<TShardingDbContext>(this DbContextOptionsBuilder optionsBuilder) where TShardingDbContext:DbContext,IShardingDbContext
|
||||
{
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
#if EFCORE2
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ShardingCore.DbContexts
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/9/5 21:29:47
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
class EFCore2DbContextLocation
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
|
@ -12,6 +12,7 @@ namespace ShardingCore.EFCores
|
|||
* @Date: Saturday, 14 August 2021 10:17:43
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
#if !EFCORE2
|
||||
public class ShardingDbSetSource:IDbSetSource
|
||||
{
|
||||
#if EFCORE5
|
||||
|
@ -80,5 +81,53 @@ namespace ShardingCore.EFCores
|
|||
=> c => new ShardingInternalDbSet<TEntity>(c);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if EFCORE2
|
||||
|
||||
/// <summary>
|
||||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
|
||||
/// directly from your code. This API may change or be removed in future releases.
|
||||
/// </summary>
|
||||
public class ShardingDbSetSource : IDbSetSource, IDbQuerySource
|
||||
{
|
||||
private static readonly MethodInfo _genericCreateSet
|
||||
= typeof(ShardingDbSetSource).GetTypeInfo().GetDeclaredMethod(nameof(CreateSetFactory));
|
||||
|
||||
private static readonly MethodInfo _genericCreateQuery
|
||||
= typeof(ShardingDbSetSource).GetTypeInfo().GetDeclaredMethod(nameof(CreateQueryFactory));
|
||||
|
||||
private readonly ConcurrentDictionary<Type, Func<DbContext, object>> _cache
|
||||
= new ConcurrentDictionary<Type, Func<DbContext, object>>();
|
||||
|
||||
/// <summary>
|
||||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
|
||||
/// directly from your code. This API may change or be removed in future releases.
|
||||
/// </summary>
|
||||
public virtual object Create(DbContext context, Type type)
|
||||
=> CreateCore(context, type, _genericCreateSet);
|
||||
|
||||
/// <summary>
|
||||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
|
||||
/// directly from your code. This API may change or be removed in future releases.
|
||||
/// </summary>
|
||||
public virtual object CreateQuery(DbContext context, Type type)
|
||||
=> CreateCore(context, type, _genericCreateQuery);
|
||||
|
||||
private object CreateCore(DbContext context, Type type, MethodInfo createMethod)
|
||||
=> _cache.GetOrAdd(
|
||||
type,
|
||||
t => (Func<DbContext, object>)createMethod
|
||||
.MakeGenericMethod(t)
|
||||
.Invoke(null, null))(context);
|
||||
|
||||
private static Func<DbContext, object> CreateSetFactory<TEntity>()
|
||||
where TEntity : class
|
||||
=> c => new ShardingInternalDbSet<TEntity>(c);
|
||||
|
||||
private static Func<DbContext, DbQuery<TQuery>> CreateQueryFactory<TQuery>()
|
||||
where TQuery : class
|
||||
=> c => new ShardingInternalDbQuery<TQuery>(c);
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
|
||||
namespace ShardingCore.EFCores
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/8/20 17:05:36
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
#if EFCORE2
|
||||
|
||||
public class ShardingInternalDbQuery<TQuery> : InternalDbQuery<TQuery> where TQuery : class
|
||||
{
|
||||
public ShardingInternalDbQuery(DbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -55,6 +55,7 @@ namespace ShardingCore.EFCores
|
|||
/// any release. You should only use it directly in your code with extreme caution and knowing that
|
||||
/// doing so can result in application failures when updating to a new Entity Framework Core release.
|
||||
/// </summary>
|
||||
#if !EFCORE2
|
||||
public override async ValueTask<EntityEntry<TEntity>> AddAsync(
|
||||
TEntity entity,
|
||||
CancellationToken cancellationToken = default)
|
||||
|
@ -63,6 +64,15 @@ namespace ShardingCore.EFCores
|
|||
return await genericDbContext.AddAsync(entity, cancellationToken);
|
||||
|
||||
}
|
||||
#endif
|
||||
#if EFCORE2
|
||||
public override async Task<EntityEntry<TEntity>> AddAsync(TEntity entity, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity);
|
||||
return await genericDbContext.AddAsync(entity, cancellationToken);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
|
||||
|
|
|
@ -15,6 +15,9 @@ using System.Threading;
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
|
||||
#if EFCORE2
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
#endif
|
||||
|
||||
namespace ShardingCore.EFCores
|
||||
{
|
||||
|
@ -41,6 +44,7 @@ namespace ShardingCore.EFCores
|
|||
return _shardingQueryExecutor.Execute<TResult>(_currentContext, query);
|
||||
}
|
||||
|
||||
#if !EFCORE2
|
||||
|
||||
public TResult ExecuteAsync<TResult>(Expression query, CancellationToken cancellationToken)
|
||||
{
|
||||
|
@ -62,6 +66,35 @@ namespace ShardingCore.EFCores
|
|||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if EFCORE2
|
||||
|
||||
|
||||
public IAsyncEnumerable<TResult> ExecuteAsync<TResult>(Expression query)
|
||||
{
|
||||
return _shardingQueryExecutor.ExecuteAsync<IAsyncEnumerable<TResult>>(_currentContext, query);
|
||||
}
|
||||
|
||||
public Task<TResult> ExecuteAsync<TResult>(Expression query, CancellationToken cancellationToken)
|
||||
{
|
||||
return _shardingQueryExecutor.ExecuteAsync<Task<TResult>>(_currentContext, query, cancellationToken);
|
||||
}
|
||||
|
||||
public Func<QueryContext, TResult> CreateCompiledQuery<TResult>(Expression query)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Func<QueryContext, IAsyncEnumerable<TResult>> CreateCompiledAsyncEnumerableQuery<TResult>(Expression query)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Func<QueryContext, Task<TResult>> CreateCompiledAsyncTaskQuery<TResult>(Expression query)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
|
@ -28,22 +28,36 @@ namespace ShardingCore.EFCores
|
|||
private readonly IRelationalConnection _relationalConnection;
|
||||
|
||||
|
||||
#if !EFCORE2
|
||||
public ShardingRelationalConnection(IRelationalConnection _relationalConnection, DbTransaction transaction)
|
||||
{
|
||||
this._relationalConnection = _relationalConnection;
|
||||
((IShardingTransaction)Context).UseShardingTransaction(transaction);
|
||||
}
|
||||
|
||||
#endif
|
||||
#if EFCORE2
|
||||
private readonly Type _dbContextType;
|
||||
public ShardingRelationalConnection(IRelationalConnection _relationalConnection,DbTransaction transaction,Type dbContextType)
|
||||
{
|
||||
this._relationalConnection = _relationalConnection;
|
||||
_dbContextType = dbContextType;
|
||||
((IShardingTransaction)Context).UseShardingTransaction(transaction);
|
||||
}
|
||||
#endif
|
||||
|
||||
public void ResetState()
|
||||
{
|
||||
_relationalConnection.ResetState();
|
||||
}
|
||||
|
||||
#if !EFCORE2
|
||||
public Task ResetStateAsync(CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
return _relationalConnection.ResetStateAsync(cancellationToken);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
public IDbContextTransaction BeginTransaction()
|
||||
{
|
||||
|
@ -122,7 +136,31 @@ namespace ShardingCore.EFCores
|
|||
public DbConnection DbConnection => _relationalConnection.DbConnection;
|
||||
|
||||
public DbContext Context =>
|
||||
#if !EFCORE2
|
||||
_relationalConnection.Context;
|
||||
#endif
|
||||
#if EFCORE2
|
||||
GetDbContext();
|
||||
|
||||
private DbContext GetDbContext()
|
||||
{
|
||||
var namedConnectionStringResolver = ((RelationalConnectionDependencies)_relationalConnection.GetPropertyValue("Dependencies")).ConnectionStringResolver;
|
||||
var serviceProvider = (IServiceProvider)namedConnectionStringResolver.GetPropertyValue("ApplicationServiceProvider");
|
||||
var dbContext = (DbContext)serviceProvider.GetService(_dbContextType);
|
||||
return dbContext;
|
||||
}
|
||||
|
||||
|
||||
public void RegisterBufferable(IBufferable bufferable)
|
||||
{
|
||||
_relationalConnection.RegisterBufferable(bufferable);
|
||||
}
|
||||
|
||||
public Task RegisterBufferableAsync(IBufferable bufferable, CancellationToken cancellationToken)
|
||||
{
|
||||
return _relationalConnection.RegisterBufferableAsync(bufferable, cancellationToken);
|
||||
}
|
||||
#endif
|
||||
public Guid ConnectionId => _relationalConnection.ConnectionId;
|
||||
|
||||
public int? CommandTimeout
|
||||
|
@ -163,6 +201,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
|
||||
public string ConnectionString => _relationalConnection.ConnectionString;
|
||||
#if !EFCORE2
|
||||
|
||||
public async Task<IDbContextTransaction> UseTransactionAsync(DbTransaction transaction, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
|
@ -181,5 +220,6 @@ namespace ShardingCore.EFCores
|
|||
{
|
||||
return _relationalConnection.DisposeAsync();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ namespace ShardingCore.EFCores
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
#if !EFCORE2
|
||||
public class ShardingRelationalTransactionFactory: RelationalTransactionFactory
|
||||
{
|
||||
private readonly RelationalTransactionFactoryDependencies _dependencies;
|
||||
|
@ -30,4 +31,23 @@ namespace ShardingCore.EFCores
|
|||
return new RelationalTransaction(new ShardingRelationalConnection(connection, transaction), transaction, transactionId, logger, transactionOwned);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if EFCORE2
|
||||
|
||||
public class ShardingRelationalTransactionFactory<TShardingDbContext> : RelationalTransactionFactory where TShardingDbContext:DbContext,IShardingDbContext
|
||||
{
|
||||
private readonly RelationalTransactionFactoryDependencies _dependencies;
|
||||
public ShardingRelationalTransactionFactory(RelationalTransactionFactoryDependencies dependencies) : base(dependencies)
|
||||
{
|
||||
_dependencies = dependencies;
|
||||
}
|
||||
public override RelationalTransaction Create(IRelationalConnection connection, DbTransaction transaction
|
||||
, IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger,
|
||||
bool transactionOwned)
|
||||
{
|
||||
return new RelationalTransaction(new ShardingRelationalConnection(connection, transaction,typeof(TShardingDbContext)), transaction, logger,
|
||||
transactionOwned);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -108,6 +108,18 @@ var contextModelRelationalModel = contextModel.RelationalModel as RelationalMode
|
|||
IMemoryCache memoryCache = modelSourceDependencies.MemoryCache;
|
||||
object key1 = modelSourceDependencies.ModelCacheKeyFactory.Create(dbContext);
|
||||
memoryCache.Remove(key1);
|
||||
#endif
|
||||
#if EFCORE2
|
||||
|
||||
var modelSource = serviceScope.ServiceProvider.GetService<IModelSource>();
|
||||
var modelSourceImpl = modelSource as RelationalModelSource;
|
||||
|
||||
var modelSourceDependencies =
|
||||
modelSourceImpl.GetPropertyValue("Dependencies") as ModelSourceDependencies;
|
||||
var models =
|
||||
typeof(ModelSource).GetTypeFieldValue(modelSourceImpl, "_models") as ConcurrentDictionary<object, Lazy<IModel>>;
|
||||
object key1 = modelSourceDependencies.ModelCacheKeyFactory.Create(dbContext);
|
||||
models.TryRemove(key1,out var del);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -133,6 +145,9 @@ var contextModelRelationalModel = contextModel.RelationalModel as RelationalMode
|
|||
var syncObject = modelSourceImpl.GetFieldValue("_syncObject");
|
||||
return syncObject;
|
||||
#endif
|
||||
#if EFCORE2
|
||||
return sLock;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -57,7 +57,13 @@ namespace ShardingCore.Extensions
|
|||
|
||||
public static string ShardingPrint(this Expression expression)
|
||||
{
|
||||
|
||||
#if !EFCORE2
|
||||
return expression.Print();
|
||||
#endif
|
||||
#if EFCORE2
|
||||
return expression.ToString();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -215,6 +215,7 @@ namespace ShardingCore.Sharding
|
|||
return CreateGenericDbContext(entity).Add(entity);
|
||||
}
|
||||
|
||||
#if !EFCORE2
|
||||
|
||||
public override ValueTask<EntityEntry<TEntity>> AddAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
|
@ -225,6 +226,18 @@ namespace ShardingCore.Sharding
|
|||
{
|
||||
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
||||
}
|
||||
#endif
|
||||
#if EFCORE2
|
||||
public override Task<EntityEntry<TEntity>> AddAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
||||
}
|
||||
|
||||
public override Task<EntityEntry> AddAsync(object entity, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
||||
}
|
||||
#endif
|
||||
|
||||
public override void AddRange(params object[] entities)
|
||||
{
|
||||
|
@ -507,6 +520,7 @@ namespace ShardingCore.Sharding
|
|||
|
||||
return i;
|
||||
}
|
||||
#if !EFCORE2
|
||||
|
||||
|
||||
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
|
||||
|
@ -537,6 +551,37 @@ namespace ShardingCore.Sharding
|
|||
|
||||
return i;
|
||||
}
|
||||
#endif
|
||||
#if EFCORE2
|
||||
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
|
||||
var isBeginTransaction = IsBeginTransaction;
|
||||
int i = 0;
|
||||
//如果是内部开的事务就内部自己消化
|
||||
if (!isBeginTransaction)
|
||||
{
|
||||
using (var tran = await Database.BeginTransactionAsync(cancellationToken))
|
||||
{
|
||||
foreach (var dbContextCache in _dbContextCaches)
|
||||
{
|
||||
i += await dbContextCache.Value.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
tran.Commit();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var dbContextCache in _dbContextCaches)
|
||||
{
|
||||
i += await dbContextCache.Value.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
#endif
|
||||
#if !EFCORE2
|
||||
|
||||
public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
|
@ -569,6 +614,37 @@ namespace ShardingCore.Sharding
|
|||
|
||||
return i;
|
||||
}
|
||||
#endif
|
||||
#if EFCORE2
|
||||
public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
|
||||
var isBeginTransaction = IsBeginTransaction;
|
||||
int i = 0;
|
||||
//如果是内部开的事务就内部自己消化
|
||||
if (!isBeginTransaction)
|
||||
{
|
||||
using (var tran = await Database.BeginTransactionAsync(cancellationToken))
|
||||
{
|
||||
|
||||
foreach (var dbContextCache in _dbContextCaches)
|
||||
{
|
||||
i += await dbContextCache.Value.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
||||
}
|
||||
|
||||
tran.Commit();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var dbContextCache in _dbContextCaches)
|
||||
{
|
||||
i += await dbContextCache.Value.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
#endif
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
|
@ -587,6 +663,7 @@ namespace ShardingCore.Sharding
|
|||
base.Dispose();
|
||||
}
|
||||
|
||||
#if !EFCORE2
|
||||
public override async ValueTask DisposeAsync()
|
||||
{
|
||||
foreach (var dbContextCache in _dbContextCaches)
|
||||
|
@ -603,5 +680,6 @@ namespace ShardingCore.Sharding
|
|||
|
||||
await base.DisposeAsync();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
|
@ -2,6 +2,9 @@ using System.Linq.Expressions;
|
|||
using System.Threading;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using ShardingCore.Sharding.Enumerators;
|
||||
#if EFCORE2
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
#endif
|
||||
|
||||
namespace ShardingCore.Sharding.Abstractions
|
||||
{
|
||||
|
|
|
@ -24,6 +24,7 @@ namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync
|
|||
{
|
||||
_inMemoryStreamMergeAsyncEnumerator = inMemoryStreamMergeAsyncEnumerator;
|
||||
}
|
||||
#if !EFCORE2
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await _inMemoryStreamMergeAsyncEnumerator.DisposeAsync();
|
||||
|
@ -46,6 +47,25 @@ namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync
|
|||
|
||||
return _reverseEnumerator.MoveNext();
|
||||
}
|
||||
#endif
|
||||
#if EFCORE2
|
||||
public async Task<bool> MoveNext(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_first)
|
||||
{
|
||||
LinkedList<T> _reverseCollection = new LinkedList<T>();
|
||||
while (await _inMemoryStreamMergeAsyncEnumerator.MoveNext(cancellationToken))
|
||||
{
|
||||
_reverseCollection.AddFirst(_inMemoryStreamMergeAsyncEnumerator.GetCurrent());
|
||||
}
|
||||
|
||||
_reverseEnumerator = _reverseCollection.GetEnumerator();
|
||||
_first = false;
|
||||
}
|
||||
|
||||
return _reverseEnumerator.MoveNext();
|
||||
}
|
||||
#endif
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
|
|
|
@ -56,11 +56,21 @@ namespace ShardingCore.Sharding.Enumerators
|
|||
return _mergeContext.SelectContext.SelectProperties.Where(o => !o.IsAggregateMethod)
|
||||
.Select(o => first.GetValueByExpression(o.PropertyName)).ToList();
|
||||
}
|
||||
#if !EFCORE2
|
||||
public async ValueTask<bool> MoveNextAsync()
|
||||
#endif
|
||||
#if EFCORE2
|
||||
public async Task<bool> MoveNext(CancellationToken cancellationToken=new CancellationToken())
|
||||
#endif
|
||||
{
|
||||
if (_queue.IsEmpty())
|
||||
return false;
|
||||
#if !EFCORE2
|
||||
var hasNext = await SetCurrentValueAsync();
|
||||
#endif
|
||||
#if EFCORE2
|
||||
var hasNext = await SetCurrentValueAsync(cancellationToken);
|
||||
#endif
|
||||
if (hasNext)
|
||||
{
|
||||
CurrentGroupValues = _queue.IsEmpty() ? new List<object>(0) : GetCurrentGroupValues(_queue.Peek());
|
||||
|
@ -79,7 +89,12 @@ namespace ShardingCore.Sharding.Enumerators
|
|||
|
||||
return true;
|
||||
}
|
||||
#if !EFCORE2
|
||||
private async ValueTask<bool> SetCurrentValueAsync()
|
||||
#endif
|
||||
#if EFCORE2
|
||||
private async Task<bool> SetCurrentValueAsync(CancellationToken cancellationToken=new CancellationToken())
|
||||
#endif
|
||||
{
|
||||
CurrentValue = default;
|
||||
var currentValues = new List<T>();
|
||||
|
@ -89,7 +104,12 @@ namespace ShardingCore.Sharding.Enumerators
|
|||
currentValues.Add(current);
|
||||
var first = _queue.Poll();
|
||||
|
||||
#if !EFCORE2
|
||||
if (await first.MoveNextAsync())
|
||||
#endif
|
||||
#if EFCORE2
|
||||
if (await first.MoveNext(cancellationToken))
|
||||
#endif
|
||||
{
|
||||
_queue.Offer(first);
|
||||
}
|
||||
|
@ -203,6 +223,7 @@ namespace ShardingCore.Sharding.Enumerators
|
|||
return CurrentValue;
|
||||
}
|
||||
|
||||
#if !EFCORE2
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
|
@ -211,6 +232,7 @@ namespace ShardingCore.Sharding.Enumerators
|
|||
await enumerator.DisposeAsync();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
public void Reset()
|
||||
|
|
|
@ -45,7 +45,12 @@ namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync
|
|||
|
||||
_currentEnumerator = _queue.IsEmpty() ? _enumerators.FirstOrDefault() : _queue.Peek();
|
||||
}
|
||||
#if !EFCORE2
|
||||
public async ValueTask<bool> MoveNextAsync()
|
||||
#endif
|
||||
#if EFCORE2
|
||||
public async Task<bool> MoveNext(CancellationToken cancellationToken = new CancellationToken())
|
||||
#endif
|
||||
{
|
||||
if (_queue.IsEmpty())
|
||||
return false;
|
||||
|
@ -56,8 +61,14 @@ namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync
|
|||
}
|
||||
|
||||
var first = _queue.Poll();
|
||||
#if !EFCORE2
|
||||
|
||||
if (await first.MoveNextAsync())
|
||||
#endif
|
||||
#if EFCORE2
|
||||
|
||||
if (await first.MoveNext(cancellationToken))
|
||||
#endif
|
||||
{
|
||||
_queue.Offer(first);
|
||||
}
|
||||
|
@ -117,6 +128,7 @@ namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync
|
|||
return _currentEnumerator.GetCurrent();
|
||||
}
|
||||
|
||||
#if !EFCORE2
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
|
@ -125,6 +137,7 @@ namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync
|
|||
await enumerator.DisposeAsync();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -38,9 +38,19 @@ namespace ShardingCore.Sharding.Enumerators
|
|||
_orderValues = HasElement() ? GetCurrentOrderValues() : new List<IComparable>(0);
|
||||
}
|
||||
|
||||
#if !EFCORE2
|
||||
public async ValueTask<bool> MoveNextAsync()
|
||||
#endif
|
||||
#if EFCORE2
|
||||
public async Task<bool> MoveNext(CancellationToken cancellationToken = new CancellationToken())
|
||||
#endif
|
||||
{
|
||||
#if !EFCORE2
|
||||
var has = await _enumerator.MoveNextAsync();
|
||||
#endif
|
||||
#if EFCORE2
|
||||
var has = await _enumerator.MoveNext(cancellationToken);
|
||||
#endif
|
||||
SetOrderValues();
|
||||
return has;
|
||||
}
|
||||
|
@ -119,10 +129,12 @@ namespace ShardingCore.Sharding.Enumerators
|
|||
{
|
||||
return _orderValues ?? new List<IComparable>(0);
|
||||
}
|
||||
#if !EFCORE2
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
return _enumerator.DisposeAsync();
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
|
@ -31,19 +31,34 @@ namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync
|
|||
else
|
||||
_enumerator = new MultiOrderStreamMergeAsyncEnumerator<T>(_mergeContext, sources);
|
||||
}
|
||||
#if !EFCORE2
|
||||
public async ValueTask<bool> MoveNextAsync()
|
||||
#endif
|
||||
#if EFCORE2
|
||||
public async Task<bool> MoveNext(CancellationToken cancellationToken = new CancellationToken())
|
||||
#endif
|
||||
{
|
||||
//如果合并数据的时候不需要跳过也没有take多少那么就是直接next
|
||||
while (_skip.GetValueOrDefault() > this.realSkip)
|
||||
{
|
||||
#if !EFCORE2
|
||||
var has = await _enumerator.MoveNextAsync();
|
||||
#endif
|
||||
#if EFCORE2
|
||||
var has = await _enumerator.MoveNext(cancellationToken);
|
||||
#endif
|
||||
|
||||
realSkip++;
|
||||
if (!has)
|
||||
return false;
|
||||
}
|
||||
|
||||
#if !EFCORE2
|
||||
var next = await _enumerator.MoveNextAsync();
|
||||
#endif
|
||||
#if EFCORE2
|
||||
var next = await _enumerator.MoveNext(cancellationToken);
|
||||
#endif
|
||||
|
||||
if (next)
|
||||
{
|
||||
|
@ -111,9 +126,12 @@ namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync
|
|||
{
|
||||
_enumerator.Dispose();
|
||||
}
|
||||
#if !EFCORE2
|
||||
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
return _enumerator.DisposeAsync();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
|
@ -43,6 +43,7 @@ namespace ShardingCore.Sharding.Enumerators
|
|||
}
|
||||
return false;
|
||||
}
|
||||
#if !EFCORE2
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_asyncSource != null)
|
||||
|
@ -95,6 +96,7 @@ namespace ShardingCore.Sharding.Enumerators
|
|||
return _syncSource.MoveNext();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
@ -104,5 +106,68 @@ namespace ShardingCore.Sharding.Enumerators
|
|||
}
|
||||
|
||||
object IEnumerator.Current => Current;
|
||||
#if EFCORE2
|
||||
public void Dispose()
|
||||
{
|
||||
_asyncSource.Dispose();
|
||||
}
|
||||
|
||||
public async Task<bool> MoveNext(CancellationToken cancellationToken=new CancellationToken())
|
||||
{
|
||||
if (skip)
|
||||
{
|
||||
skip = false;
|
||||
return null != SourceCurrent();
|
||||
}
|
||||
return await _asyncSource.MoveNext(cancellationToken);
|
||||
}
|
||||
public T Current => GetCurrent();
|
||||
public T ReallyCurrent => GetReallyCurrent();
|
||||
public bool HasElement()
|
||||
{
|
||||
return null != SourceCurrent();
|
||||
}
|
||||
|
||||
private T SourceCurrent()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (tryGetCurrentError)
|
||||
return default;
|
||||
return _asyncSource.Current;
|
||||
}catch(Exception e)
|
||||
{
|
||||
tryGetCurrentError = true;
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
private bool tryGetCurrentError = false;
|
||||
|
||||
public T GetCurrent()
|
||||
{
|
||||
if (skip)
|
||||
return default;
|
||||
if (_asyncSource != null) return SourceCurrent();
|
||||
if (_syncSource != null) return _syncSource.Current;
|
||||
return default;
|
||||
}
|
||||
public T GetReallyCurrent()
|
||||
{
|
||||
if (_asyncSource != null) return SourceCurrent();
|
||||
if (_syncSource != null) return _syncSource.Current;
|
||||
return default;
|
||||
}
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (skip)
|
||||
{
|
||||
skip = false;
|
||||
return null != _syncSource.Current;
|
||||
}
|
||||
return _syncSource.MoveNext();
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ShardingCore.Sharding.PaginationConfigurations.MultiQueryPagination
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/9/15 17:07:30
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public interface IMultiQueryPredicate
|
||||
{
|
||||
public bool ContinueMultiQuery(long total, int currentSkip, int tables);
|
||||
}
|
||||
}
|
|
@ -53,14 +53,5 @@ namespace ShardingCore.Sharding.PaginationConfigurations
|
|||
// _metadata.TakeInMemoryCountIfLe = count;
|
||||
// return this;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 启用多次查询排序
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public PaginationBuilder<TEntity> ConfigMultiQueryShardingPage()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,9 @@ using ShardingCore.Sharding.Abstractions;
|
|||
using ShardingCore.Sharding.StreamMergeEngines;
|
||||
using ShardingCore.Sharding.StreamMergeEngines.Abstractions;
|
||||
using ShardingCore.Sharding.StreamMergeEngines.AggregateMergeEngines;
|
||||
#if EFCORE2
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
#endif
|
||||
|
||||
namespace ShardingCore.Sharding.ShardingQueryExecutors
|
||||
{
|
||||
|
|
|
@ -3,7 +3,6 @@ using System.Linq;
|
|||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using ShardingCore.Exceptions;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.Sharding.StreamMergeEngines.Abstractions.AbstractEnsureExpressionMergeEngines
|
||||
|
@ -27,7 +26,12 @@ namespace ShardingCore.Sharding.StreamMergeEngines.Abstractions.AbstractEnsureEx
|
|||
if (!(secondExpression is ConstantExpression constantExpression))
|
||||
{
|
||||
|
||||
throw new ShardingCoreException($"not found constant {methodCallExpression.ShardingPrint()}");
|
||||
#if !EFCORE2
|
||||
throw new ShardingCoreException($"not found constant {methodCallExpression.Print()}");
|
||||
#endif
|
||||
#if EFCORE2
|
||||
throw new ShardingCoreException($"not found constant {methodCallExpression}");
|
||||
#endif
|
||||
}
|
||||
_constantItem = (TEntity)constantExpression.Value;
|
||||
}
|
||||
|
@ -35,7 +39,12 @@ namespace ShardingCore.Sharding.StreamMergeEngines.Abstractions.AbstractEnsureEx
|
|||
{
|
||||
if (!(secondExpression is ConstantExpression))
|
||||
{
|
||||
throw new InvalidOperationException(_methodCallExpression.ShardingPrint());
|
||||
#if !EFCORE2
|
||||
throw new InvalidOperationException(_methodCallExpression.Print());
|
||||
#endif
|
||||
#if EFCORE2
|
||||
throw new InvalidOperationException(_methodCallExpression.ToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
return queryable;
|
||||
|
|
|
@ -3,7 +3,6 @@ using System.Linq;
|
|||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using ShardingCore.Exceptions;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.Sharding.StreamMergeEngines.Abstractions.AbstractEnsureExpressionMergeEngines
|
||||
|
@ -32,7 +31,12 @@ namespace ShardingCore.Sharding.StreamMergeEngines.Abstractions.AbstractEnsureEx
|
|||
return queryable.Select(selector);
|
||||
}
|
||||
|
||||
throw new ShardingCoreException($"expression is not selector:{secondExpression.ShardingPrint()}");
|
||||
#if !EFCORE2
|
||||
throw new ShardingCoreException($"expression is not selector:{secondExpression.Print()}");
|
||||
#endif
|
||||
#if EFCORE2
|
||||
throw new ShardingCoreException($"expression is not selector:{secondExpression}");
|
||||
#endif
|
||||
}
|
||||
return queryable;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.Sharding.StreamMergeEngines.Abstractions.AbstractEnsureExpressionMergeEngines
|
||||
|
@ -30,7 +29,12 @@ namespace ShardingCore.Sharding.StreamMergeEngines.Abstractions.AbstractEnsureEx
|
|||
return queryable.Where(predicate);
|
||||
}
|
||||
|
||||
throw new InvalidOperationException(_methodCallExpression.ShardingPrint());
|
||||
#if !EFCORE2
|
||||
throw new InvalidOperationException(_methodCallExpression.Print());
|
||||
#endif
|
||||
#if EFCORE2
|
||||
throw new InvalidOperationException(_methodCallExpression.ToString());
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ using System.Linq;
|
|||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using ShardingCore.Exceptions;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.Sharding.StreamMergeEngines.Abstractions.AbstractGenericExpressionMergeEngines
|
||||
|
@ -31,7 +30,13 @@ namespace ShardingCore.Sharding.StreamMergeEngines.Abstractions.AbstractGenericE
|
|||
return queryable.Select(selector);
|
||||
}
|
||||
|
||||
throw new ShardingCoreException($"expression is not selector:{secondExpression.ShardingPrint()}");
|
||||
|
||||
#if !EFCORE2
|
||||
throw new ShardingCoreException($"expression is not selector:{secondExpression.Print()}");
|
||||
#endif
|
||||
#if EFCORE2
|
||||
throw new ShardingCoreException($"expression is not selector:{secondExpression}");
|
||||
#endif
|
||||
}
|
||||
return queryable;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.Sharding.StreamMergeEngines.Abstractions.AbstractGenericExpressionMergeEngines
|
||||
|
@ -32,7 +31,12 @@ namespace ShardingCore.Sharding.StreamMergeEngines.Abstractions.AbstractGenericE
|
|||
return queryable.Where(predicate);
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException(_methodCallExpression.ShardingPrint());
|
||||
#if !EFCORE2
|
||||
throw new InvalidOperationException(_methodCallExpression.Print());
|
||||
#endif
|
||||
#if EFCORE2
|
||||
throw new InvalidOperationException(_methodCallExpression.ToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,6 +3,10 @@ using System.Collections;
|
|||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
#if EFCORE2
|
||||
using Microsoft.EntityFrameworkCore.Extensions.Internal;
|
||||
#endif
|
||||
|
||||
namespace ShardingCore.Sharding.StreamMergeEngines
|
||||
{
|
||||
/*
|
||||
|
@ -21,11 +25,21 @@ namespace ShardingCore.Sharding.StreamMergeEngines
|
|||
}
|
||||
|
||||
|
||||
#if !EFCORE2
|
||||
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
return new EnumeratorShardingQueryExecutor<T>(_mergeContext).ExecuteAsync(cancellationToken)
|
||||
.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if EFCORE2
|
||||
IAsyncEnumerator<T> IAsyncEnumerable<T>.GetEnumerator()
|
||||
{
|
||||
return ((IAsyncEnumerable<T>)new EnumeratorShardingQueryExecutor<T>(_mergeContext).ExecuteAsync())
|
||||
.GetEnumerator();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
|
|
|
@ -10,6 +10,9 @@ using ShardingCore.Exceptions;
|
|||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Sharding.Enumerators;
|
||||
using ShardingCore.Sharding.Enumerators.StreamMergeAsync;
|
||||
#if EFCORE2
|
||||
using Microsoft.EntityFrameworkCore.Extensions.Internal;
|
||||
#endif
|
||||
|
||||
namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.Abstractions
|
||||
{
|
||||
|
@ -57,9 +60,16 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
|
|||
}
|
||||
public async Task<IAsyncEnumerator<TEntity>> DoGetAsyncEnumerator(IQueryable<TEntity> newQueryable)
|
||||
{
|
||||
#if !EFCORE2
|
||||
var enumator = newQueryable.AsAsyncEnumerable().GetAsyncEnumerator();
|
||||
await enumator.MoveNextAsync();
|
||||
return enumator;
|
||||
#endif
|
||||
#if EFCORE2
|
||||
var enumator = newQueryable.AsAsyncEnumerable().GetEnumerator();
|
||||
await enumator.MoveNext();
|
||||
return enumator;
|
||||
#endif
|
||||
}
|
||||
public IEnumerator<TEntity> DoGetEnumerator(IQueryable<TEntity> newQueryable)
|
||||
{
|
||||
|
|
|
@ -32,11 +32,21 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
|
|||
public abstract IStreamMergeAsyncEnumerator<TEntity> GetShardingAsyncEnumerator(bool async,
|
||||
CancellationToken cancellationToken = new CancellationToken());
|
||||
|
||||
#if !EFCORE2
|
||||
public IAsyncEnumerator<TEntity> GetAsyncEnumerator(
|
||||
CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
return GetShardingAsyncEnumerator(true,cancellationToken);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if EFCORE2
|
||||
IAsyncEnumerator<TEntity> IAsyncEnumerable<TEntity>.GetEnumerator()
|
||||
{
|
||||
return GetShardingAsyncEnumerator(true);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
public IEnumerator<TEntity> GetEnumerator()
|
||||
{
|
||||
|
|
|
@ -70,7 +70,12 @@ namespace ShardingCore
|
|||
var virtualTable = CreateVirtualTable(entity.ClrType, virtualRoute);
|
||||
|
||||
//获取ShardingEntity的实际表名
|
||||
#if !EFCORE2
|
||||
var tableName = context.Model.FindEntityType(virtualTable.EntityType).GetTableName();
|
||||
#endif
|
||||
#if EFCORE2
|
||||
var tableName = context.Model.FindEntityType(virtualTable.EntityType).Relational().TableName;
|
||||
#endif
|
||||
virtualTable.SetOriginalTableName(tableName);
|
||||
_virtualTableManager.AddVirtualTable(shardingConfigOption.ShardingDbContextType,virtualTable);
|
||||
CreateDataTable(shardingConfigOption.ShardingDbContextType,virtualTable, shardingConfigOption);
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Version>$(EFCORE5)</Version>
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>$(EFCORE2)</Version>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<DefineConstants>TRACE;DEBUG;EFCORE2;</DefineConstants>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<AssemblyName>ShardingCore</AssemblyName>
|
||||
<RootNamespace>ShardingCore</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<DocumentationFile>bin\Release\ShardingCore.2x.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\..\src\ShardingCore\**\*.cs" />
|
||||
<Compile Remove="..\..\src\ShardingCore\obj\**" />
|
||||
<Compile Remove="..\..\src\ShardingCore\bin\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.2.6" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,8 @@
|
|||
using System;
|
||||
|
||||
namespace ShardingCore.Test50_2x
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"SqlServer": {
|
||||
"ConnectionString": "Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.VirtualRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Test50_2x.Domain.Maps;
|
||||
|
||||
namespace ShardingCore.Test50_2x
|
||||
{
|
||||
/*
|
||||
* @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());
|
||||
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
using ShardingCore.Core;
|
||||
|
||||
namespace ShardingCore.Test50_2x.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,42 @@
|
|||
using ShardingCore.Core;
|
||||
|
||||
namespace ShardingCore.Test50_2x.Domain.Entities
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Monday, 01 February 2021 15:43:22
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class SysUserSalary:IShardingTable
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string UserId { get; set; }
|
||||
/// <summary>
|
||||
/// 每月的金额
|
||||
/// </summary>
|
||||
[ShardingTableKey]
|
||||
public int DateOfMonth { get; set; }
|
||||
/// <summary>
|
||||
/// 工资
|
||||
/// </summary>
|
||||
public int Salary { get; set; }
|
||||
/// <summary>
|
||||
/// 工资
|
||||
/// </summary>
|
||||
public long SalaryLong { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 工资
|
||||
/// </summary>
|
||||
public decimal SalaryDecimal { get; set; }
|
||||
/// <summary>
|
||||
/// 工资
|
||||
/// </summary>
|
||||
public double SalaryDouble { get; set; }
|
||||
/// <summary>
|
||||
/// 工资
|
||||
/// </summary>
|
||||
public float SalaryFloat { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using ShardingCore.Test50_2x.Domain.Entities;
|
||||
|
||||
namespace ShardingCore.Test50_2x.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,23 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using ShardingCore.Test50_2x.Domain.Entities;
|
||||
|
||||
namespace ShardingCore.Test50_2x.Domain.Maps
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Monday, 01 February 2021 15:42:35
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class SysUserSalaryMap:IEntityTypeConfiguration<SysUserSalary>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<SysUserSalary> builder)
|
||||
{
|
||||
builder.HasKey(o => o.Id);
|
||||
builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
|
||||
builder.Property(o => o.UserId).IsRequired().HasMaxLength(128);
|
||||
builder.ToTable(nameof(SysUserSalary));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<LangVersion>9.0</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="5.0.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.6" />
|
||||
<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>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Configs\DbConfig.json">
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</Content>
|
||||
<None Remove="Configs\MacDbConfig.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src2x\ShardingCore.2x\ShardingCore.2x.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Sharding;
|
||||
using ShardingCore.Test50_2x.Domain.Maps;
|
||||
|
||||
namespace ShardingCore.Test50_2x
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/8/15 10:21:03
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingDefaultDbContext:AbstractShardingDbContext<DefaultDbContext>
|
||||
{
|
||||
public ShardingDefaultDbContext(DbContextOptions<ShardingDefaultDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new SysUserModMap());
|
||||
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
|
||||
}
|
||||
|
||||
public override Type ShardingDbContextType => this.GetType();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,417 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.QueryRouteManagers.Abstractions;
|
||||
using ShardingCore.Test50_2x.Domain.Entities;
|
||||
using Xunit;
|
||||
|
||||
namespace ShardingCore.Test50_2x
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Friday, 15 January 2021 17:22:10
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingTest
|
||||
{
|
||||
private readonly ShardingDefaultDbContext _virtualDbContext;
|
||||
private readonly IShardingRouteManager _shardingRouteManager;
|
||||
|
||||
public ShardingTest(ShardingDefaultDbContext virtualDbContext,IShardingRouteManager shardingRouteManager)
|
||||
{
|
||||
_virtualDbContext = virtualDbContext;
|
||||
_shardingRouteManager = shardingRouteManager;
|
||||
}
|
||||
|
||||
//[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_Route_Test()
|
||||
{
|
||||
using (_shardingRouteManager.CreateScope())
|
||||
{
|
||||
_shardingRouteManager.Current.Must.TryAdd(typeof(SysUserMod), new HashSet<string>() { "00" });
|
||||
|
||||
var mod00s = await _virtualDbContext.Set<SysUserMod>().ToListAsync();
|
||||
Assert.Equal(333, mod00s.Count);
|
||||
}
|
||||
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--;
|
||||
}
|
||||
}
|
||||
|
||||
[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)
|
||||
{
|
||||
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 pageResult = await _virtualDbContext.Set<SysUserMod>().Skip(10).Take(10).OrderByDescending(o => o.Age).ToListAsync();
|
||||
Assert.Equal(10, pageResult.Count);
|
||||
int pageDescAge = 990;
|
||||
foreach (var sysUserMod in pageResult)
|
||||
{
|
||||
Assert.Equal(pageDescAge, sysUserMod.Age);
|
||||
pageDescAge--;
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToList_Join_Test()
|
||||
{
|
||||
var list = await (from u in _virtualDbContext.Set<SysUserMod>()
|
||||
join salary in _virtualDbContext.Set<SysUserSalary>()
|
||||
on u.Id equals salary.UserId
|
||||
select new
|
||||
{
|
||||
u.Id,
|
||||
u.Age,
|
||||
Salary = salary.Salary,
|
||||
DateOfMonth = salary.DateOfMonth,
|
||||
Name = u.Name
|
||||
}).ToListAsync();
|
||||
var list2 = list.OrderBy(o=>o.Age).Select(o=>o.Age).Distinct().ToList();
|
||||
Assert.Equal(24000, list.Count());
|
||||
Assert.Equal(24, list.Count(o => o.Name == "name_200"));
|
||||
|
||||
|
||||
var queryable = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
|
||||
join salary in _virtualDbContext.Set<SysUserSalary>()
|
||||
on u.Id equals salary.UserId
|
||||
select new
|
||||
{
|
||||
Salary = salary.Salary,
|
||||
DateOfMonth = salary.DateOfMonth,
|
||||
Name = u.Name
|
||||
});
|
||||
var list1 = await queryable.ToListAsync();
|
||||
Assert.Equal(24, list1.Count());
|
||||
Assert.DoesNotContain(list1, o => o.Name != "name_300");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToList_OrderBy_Asc_Desc_Test()
|
||||
{
|
||||
var modascs = await _virtualDbContext.Set<SysUserMod>().OrderBy(o => o.Age).ToListAsync();
|
||||
Assert.Equal(1000, modascs.Count);
|
||||
var i = 1;
|
||||
foreach (var age in modascs)
|
||||
{
|
||||
Assert.Equal(i, age.Age);
|
||||
i++;
|
||||
}
|
||||
|
||||
var moddescs = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Age).ToListAsync();
|
||||
Assert.Equal(1000, moddescs.Count);
|
||||
var j = 1000;
|
||||
foreach (var age in moddescs)
|
||||
{
|
||||
Assert.Equal(j, age.Age);
|
||||
j--;
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToList_Id_In_Test()
|
||||
{
|
||||
var ids = new[] {"1", "2", "3", "4"};
|
||||
var sysUserMods = await _virtualDbContext.Set<SysUserMod>().Where(o => ids.Contains(o.Id)).ToListAsync();
|
||||
foreach (var id in ids)
|
||||
{
|
||||
Assert.Contains(sysUserMods, o => o.Id == id);
|
||||
}
|
||||
|
||||
Assert.DoesNotContain(sysUserMods, o => o.Age > 4);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToList_Id_Eq_Test()
|
||||
{
|
||||
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "3").ToListAsync();
|
||||
Assert.Single(mods);
|
||||
Assert.Equal("3", mods[0].Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToList_Id_Not_Eq_Test()
|
||||
{
|
||||
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").ToListAsync();
|
||||
Assert.Equal(999, mods.Count);
|
||||
Assert.DoesNotContain(mods, o => o.Id == "3");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToList_Id_Not_Eq_Skip_Test()
|
||||
{
|
||||
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").OrderBy(o => o.Age).Skip(2).ToListAsync();
|
||||
Assert.Equal(997, mods.Count);
|
||||
Assert.DoesNotContain(mods, o => o.Id == "3");
|
||||
Assert.Equal(4, mods[0].Age);
|
||||
Assert.Equal(5, mods[1].Age);
|
||||
|
||||
var modsDesc = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").OrderByDescending(o => o.Age).Skip(13).ToListAsync();
|
||||
Assert.Equal(986, modsDesc.Count);
|
||||
Assert.DoesNotContain(mods, o => o.Id == "3");
|
||||
Assert.Equal(987, modsDesc[0].Age);
|
||||
Assert.Equal(986, modsDesc[1].Age);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToList_Name_Eq_Test()
|
||||
{
|
||||
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_3").ToListAsync();
|
||||
Assert.Single(mods);
|
||||
Assert.Equal("3", mods[0].Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToList_Id_Eq_Not_In_Db_Test()
|
||||
{
|
||||
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1001").ToListAsync();
|
||||
Assert.Empty(mods);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToList_Name_Eq_Not_In_Db_Test()
|
||||
{
|
||||
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_1001").ToListAsync();
|
||||
Assert.Empty(mods);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FirstOrDefault_Order_By_Id_Test()
|
||||
{
|
||||
var sysUserModAge = await _virtualDbContext.Set<SysUserMod>().OrderBy(o => o.Age).FirstOrDefaultAsync();
|
||||
Assert.True(sysUserModAge != null && sysUserModAge.Id == "1");
|
||||
var sysUserModAgeDesc = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Age).FirstOrDefaultAsync();
|
||||
Assert.True(sysUserModAgeDesc != null && sysUserModAgeDesc.Id == "1000");
|
||||
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().OrderBy(o => o.Id).FirstOrDefaultAsync();
|
||||
Assert.True(sysUserMod != null && sysUserMod.Id == "1");
|
||||
|
||||
var sysUserModDesc = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Id).FirstOrDefaultAsync();
|
||||
Assert.True(sysUserModDesc != null && sysUserModDesc.Id == "999");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FirstOrDefault2()
|
||||
{
|
||||
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefaultAsync();
|
||||
Assert.NotNull(sysUserMod);
|
||||
Assert.True(sysUserMod.Id == "1");
|
||||
var user198 = await _virtualDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "198");
|
||||
Assert.True(user198.Id == "198");
|
||||
var userId198 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Select(o => o.Id).FirstOrDefaultAsync();
|
||||
Assert.Equal(userId198, "198");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FirstOrDefault3()
|
||||
{
|
||||
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
|
||||
Assert.NotNull(sysUserMod);
|
||||
Assert.Equal("2", sysUserMod.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FirstOrDefault4()
|
||||
{
|
||||
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "1").FirstOrDefaultAsync();
|
||||
Assert.NotNull(sysUserMod);
|
||||
Assert.True(sysUserMod.Id != "1");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FirstOrDefault5()
|
||||
{
|
||||
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_1001").FirstOrDefaultAsync();
|
||||
Assert.Null(sysUserMod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Count_Test()
|
||||
{
|
||||
var a = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_1000").CountAsync();
|
||||
Assert.Equal(1, a);
|
||||
var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1000").CountAsync();
|
||||
Assert.Equal(999, b);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Sum_Test()
|
||||
{
|
||||
var a = await _virtualDbContext.Set<SysUserMod>().SumAsync(o => o.Age);
|
||||
var expected = 0;
|
||||
for (int i = 1; i <= 1000; i++)
|
||||
{
|
||||
expected += i;
|
||||
}
|
||||
|
||||
Assert.Equal(expected, a);
|
||||
var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1000").SumAsync(o => o.Age);
|
||||
Assert.Equal(expected - 1000, b);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Max_Test()
|
||||
{
|
||||
var a = await _virtualDbContext.Set<SysUserMod>().MaxAsync(o => o.Age);
|
||||
Assert.Equal(1000, a);
|
||||
var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1000").MaxAsync(o => o.Age);
|
||||
Assert.Equal(999, b);
|
||||
var c = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age < 500).MaxAsync(o => o.Age);
|
||||
Assert.Equal(499, c);
|
||||
var e = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age <= 500).MaxAsync(o => o.Age);
|
||||
Assert.Equal(500, e);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Max_Join_Test()
|
||||
{
|
||||
var queryable = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
|
||||
join salary in _virtualDbContext.Set<SysUserSalary>()
|
||||
on u.Id equals salary.UserId
|
||||
select new
|
||||
{
|
||||
Salary = salary.Salary,
|
||||
DateOfMonth = salary.DateOfMonth,
|
||||
Name = u.Name
|
||||
});
|
||||
var maxSalary = await queryable.MaxAsync(o => o.Salary);
|
||||
Assert.Equal(1390000, maxSalary);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Min_Test()
|
||||
{
|
||||
var a = await _virtualDbContext.Set<SysUserMod>().MinAsync(o => o.Age);
|
||||
Assert.Equal(1, a);
|
||||
var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1").MinAsync(o => o.Age);
|
||||
Assert.Equal(2, b);
|
||||
var c = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age > 500).MinAsync(o => o.Age);
|
||||
Assert.Equal(501, c);
|
||||
var e = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age >= 500).MinAsync(o => o.Age);
|
||||
Assert.Equal(500, e);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Any_Test()
|
||||
{
|
||||
var a = await _virtualDbContext.Set<SysUserMod>().AnyAsync(o => o.Age == 100);
|
||||
Assert.True(a);
|
||||
var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1").AnyAsync(o => o.Age == 1);
|
||||
Assert.False(b);
|
||||
var c = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age > 500).AnyAsync(o => o.Age <= 500);
|
||||
Assert.False(c);
|
||||
var e = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age >= 500).AnyAsync(o => o.Age <= 500);
|
||||
Assert.True(e);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Group_Test()
|
||||
{
|
||||
var ids = new[] {"200", "300"};
|
||||
var dateOfMonths = new[] {202111, 202110};
|
||||
var group = await (from u in _virtualDbContext.Set<SysUserSalary>()
|
||||
.Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
|
||||
group u by new
|
||||
{
|
||||
UId = u.UserId
|
||||
}
|
||||
into g
|
||||
select new
|
||||
{
|
||||
GroupUserId = g.Key.UId,
|
||||
Count = g.Count(),
|
||||
TotalSalary = g.Sum(o => o.Salary),
|
||||
AvgSalary = g.Average(o => o.Salary),
|
||||
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
|
||||
MinSalary = g.Min(o => o.Salary),
|
||||
MaxSalary = g.Max(o => o.Salary)
|
||||
}).ToListAsync();
|
||||
Assert.Equal(2, group.Count);
|
||||
Assert.Equal(2, group[0].Count);
|
||||
Assert.Equal(2260000, group[0].TotalSalary);
|
||||
Assert.Equal(1130000, group[0].AvgSalary);
|
||||
Assert.Equal(11300, group[0].AvgSalaryDecimal);
|
||||
Assert.Equal(1120000, group[0].MinSalary);
|
||||
Assert.Equal(1140000, group[0].MaxSalary);
|
||||
}
|
||||
// [Fact]
|
||||
// public async Task Group_API_Test()
|
||||
// {
|
||||
// var ids = new[] {"200", "300"};
|
||||
// var dateOfMonths = new[] {202111, 202110};
|
||||
// var group = await _virtualDbContext.Set<SysUserSalary>()
|
||||
// .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
|
||||
// .ShardingGroupByAsync(g => new {UId = g.UserId}, g => new
|
||||
// {
|
||||
//
|
||||
// GroupUserId = g.Key.UId,
|
||||
// Count = g.Count(),
|
||||
// TotalSalary = g.Sum(o => o.Salary),
|
||||
// AvgSalary = g.Average(o => o.Salary),
|
||||
// AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
|
||||
// MinSalary = g.Min(o => o.Salary),
|
||||
// MaxSalary = g.Max(o => o.Salary)
|
||||
// });
|
||||
// Assert.Equal(2, group.Count);
|
||||
// Assert.Equal(2, group[0].Count);
|
||||
// Assert.Equal(2260000, group[0].TotalSalary);
|
||||
// Assert.Equal(1130000, group[0].AvgSalary);
|
||||
// Assert.Equal(11300, group[0].AvgSalaryDecimal);
|
||||
// Assert.Equal(1120000, group[0].MinSalary);
|
||||
// Assert.Equal(1140000, group[0].MaxSalary);
|
||||
// }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
using ShardingCore.Test50_2x.Domain.Entities;
|
||||
using ShardingCore.VirtualRoutes.Mods;
|
||||
|
||||
namespace ShardingCore.Test50_2x.Shardings
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Thursday, 14 January 2021 15:39:27
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class SysUserModVirtualTableRoute : AbstractSimpleShardingModKeyStringVirtualTableRoute<SysUserMod>
|
||||
{
|
||||
protected override bool EnableHintRoute => true;
|
||||
public SysUserModVirtualTableRoute() : base(2,3)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
|
||||
using ShardingCore.Test50_2x.Domain.Entities;
|
||||
|
||||
namespace ShardingCore.Test50_2x.Shardings
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Monday, 01 February 2021 15:54:55
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class SysUserSalaryVirtualTableRoute:AbstractShardingOperatorVirtualTableRoute<SysUserSalary,int>
|
||||
{
|
||||
protected override int ConvertToShardingKey(object shardingKey)
|
||||
{
|
||||
return Convert.ToInt32(shardingKey);
|
||||
}
|
||||
|
||||
public override string ShardingKeyToTail(object shardingKey)
|
||||
{
|
||||
var time = ConvertToShardingKey(shardingKey);
|
||||
return TimeFormatToTail(time);
|
||||
}
|
||||
|
||||
|
||||
public override List<string> GetAllTails()
|
||||
{
|
||||
var beginTime = new DateTime(2020, 1, 1);
|
||||
var endTime = new DateTime(2021, 12, 1);
|
||||
var list = new List<string>(24);
|
||||
var tempTime = beginTime;
|
||||
while (tempTime <= endTime)
|
||||
{
|
||||
list.Add($"{tempTime:yyyyMM}");
|
||||
tempTime = tempTime.AddMonths(1);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
protected string TimeFormatToTail(int time)
|
||||
{
|
||||
var dateOfMonth=DateTime.ParseExact($"{time}","yyyyMM",System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.AdjustToUniversal);
|
||||
return $"{dateOfMonth:yyyyMM}";
|
||||
}
|
||||
|
||||
protected override Expression<Func<string, bool>> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
|
||||
{
|
||||
var t = TimeFormatToTail(shardingKey);
|
||||
switch (shardingOperator)
|
||||
{
|
||||
case ShardingOperatorEnum.GreaterThan:
|
||||
case ShardingOperatorEnum.GreaterThanOrEqual:
|
||||
return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0;
|
||||
case ShardingOperatorEnum.LessThan:
|
||||
return tail => String.Compare(tail, t, StringComparison.Ordinal) < 0;
|
||||
case ShardingOperatorEnum.LessThanOrEqual:
|
||||
return tail => String.Compare(tail, t, StringComparison.Ordinal) <= 0;
|
||||
case ShardingOperatorEnum.Equal: return tail => tail == t;
|
||||
default:
|
||||
{
|
||||
#if DEBUG
|
||||
Console.WriteLine($"shardingOperator is not equal scan all table tail");
|
||||
#endif
|
||||
return tail => true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,123 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ShardingCore.EFCores;
|
||||
using ShardingCore.Test50_2x.Domain.Entities;
|
||||
using ShardingCore.Test50_2x.Shardings;
|
||||
|
||||
namespace ShardingCore.Test50_2x
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Friday, 15 January 2021 15:37:46
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class Startup
|
||||
{
|
||||
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
|
||||
{
|
||||
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
|
||||
});
|
||||
// // 自定义 host 构建
|
||||
public void ConfigureHost(IHostBuilder hostBuilder)
|
||||
{
|
||||
hostBuilder
|
||||
.ConfigureAppConfiguration(builder =>
|
||||
{
|
||||
builder.AddJsonFile("Configs/DbConfig.json");
|
||||
//builder.AddJsonFile("Configs/MacDbConfig.json");
|
||||
});
|
||||
}
|
||||
|
||||
// 支持的形式:
|
||||
// ConfigureServices(IServiceCollection services)
|
||||
// ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
|
||||
// ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services)
|
||||
public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
|
||||
{
|
||||
services.AddDbContext<DefaultDbContext>();
|
||||
services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"])
|
||||
, op =>
|
||||
{
|
||||
op.EnsureCreatedWithOutShardingTable = true;
|
||||
op.CreateShardingTableOnStart = true;
|
||||
op.UseShardingOptionsBuilder((connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger),
|
||||
(conStr,builder)=> builder.UseSqlServer(conStr).UseLoggerFactory(efLogger));
|
||||
op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
||||
op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
|
||||
});
|
||||
}
|
||||
|
||||
// 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 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<ShardingDefaultDbContext>();
|
||||
if (!await virtualDbContext.Set<SysUserMod>().AnyAsync())
|
||||
{
|
||||
var ids = Enumerable.Range(1, 1000);
|
||||
var userMods = new List<SysUserMod>();
|
||||
var userSalaries = new List<SysUserSalary>();
|
||||
var beginTime = new DateTime(2020, 1, 1);
|
||||
var endTime = new DateTime(2021, 12, 1);
|
||||
foreach (var id in ids)
|
||||
{
|
||||
userMods.Add(new SysUserMod()
|
||||
{
|
||||
Id = id.ToString(),
|
||||
Age = id,
|
||||
Name = $"name_{id}",
|
||||
AgeGroup = Math.Abs(id % 10)
|
||||
});
|
||||
var tempTime = beginTime;
|
||||
var i = 0;
|
||||
while (tempTime <= endTime)
|
||||
{
|
||||
var dateOfMonth = $@"{tempTime:yyyyMM}";
|
||||
userSalaries.Add(new SysUserSalary()
|
||||
{
|
||||
Id = $@"{id}{dateOfMonth}",
|
||||
UserId = id.ToString(),
|
||||
DateOfMonth = int.Parse(dateOfMonth),
|
||||
Salary = 700000 + id * 100 * i,
|
||||
SalaryLong = 700000 + id * 100 * i,
|
||||
SalaryDecimal = (700000 + id * 100 * i) / 100m,
|
||||
SalaryDouble = (700000 + id * 100 * i) / 100d,
|
||||
SalaryFloat = (700000 + id * 100 * i) / 100f
|
||||
});
|
||||
tempTime = tempTime.AddMonths(1);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
await virtualDbContext.AddRangeAsync(userMods);
|
||||
await virtualDbContext.AddRangeAsync(userSalaries);
|
||||
|
||||
await virtualDbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue