发布.3.1.11并且修复find未能正确路由到dbcontext的bug
This commit is contained in:
parent
7efb0697d9
commit
96c2833a49
|
@ -1,8 +1,8 @@
|
||||||
:start
|
:start
|
||||||
::定义版本
|
::定义版本
|
||||||
set EFCORE2=2.3.1.10
|
set EFCORE2=2.3.1.11
|
||||||
set EFCORE3=3.3.1.10
|
set EFCORE3=3.3.1.11
|
||||||
set EFCORE5=5.3.1.10
|
set EFCORE5=5.3.1.11
|
||||||
|
|
||||||
::删除所有bin与obj下的文件
|
::删除所有bin与obj下的文件
|
||||||
@echo off
|
@echo off
|
||||||
|
|
|
@ -60,7 +60,7 @@ namespace Samples.AbpSharding
|
||||||
|
|
||||||
//public void ShardingUpgrade()
|
//public void ShardingUpgrade()
|
||||||
//{
|
//{
|
||||||
// //isExecutor = true;
|
// //IsExecutor = true;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
public DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail)
|
public DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace ShardingCore.Core.VirtualDatabase
|
namespace ShardingCore.Core.VirtualDatabase
|
||||||
|
@ -29,6 +30,11 @@ namespace ShardingCore.Core.VirtualDatabase
|
||||||
/// 分库字段
|
/// 分库字段
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string ShardingDataSourceField { get; set; }
|
public string ShardingDataSourceField { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 主键名称
|
||||||
|
/// </summary>
|
||||||
|
public string SinglePrimaryKeyFieldName { get; set; }
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启动时是否建表 auto create data source when start app
|
/// 启动时是否建表 auto create data source when start app
|
||||||
|
|
|
@ -15,11 +15,8 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
||||||
* @Date: Friday, 05 February 2021 13:01:39
|
* @Date: Friday, 05 February 2021 13:01:39
|
||||||
* @Email: 326308290@qq.com
|
* @Email: 326308290@qq.com
|
||||||
*/
|
*/
|
||||||
/// <summary>
|
|
||||||
/// 虚拟数据源 连接所有的实际数据源
|
public interface IVirtualDataSource
|
||||||
/// </summary>
|
|
||||||
public interface IVirtualDataSource<TShardingDbContext>
|
|
||||||
where TShardingDbContext : DbContext, IShardingDbContext
|
|
||||||
{
|
{
|
||||||
string DefaultDataSourceName { get; }
|
string DefaultDataSourceName { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -55,4 +52,11 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
||||||
bool AddVirtualDataSourceRoute(IVirtualDataSourceRoute virtualDataSourceRoute);
|
bool AddVirtualDataSourceRoute(IVirtualDataSourceRoute virtualDataSourceRoute);
|
||||||
bool IsDefault(string dataSourceName);
|
bool IsDefault(string dataSourceName);
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 虚拟数据源 连接所有的实际数据源
|
||||||
|
/// </summary>
|
||||||
|
public interface IVirtualDataSource<TShardingDbContext> : IVirtualDataSource
|
||||||
|
where TShardingDbContext : DbContext, IShardingDbContext
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -8,6 +8,12 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||||
|
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||||
|
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails;
|
||||||
|
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||||
|
using ShardingCore.Extensions;
|
||||||
|
using ShardingCore.Utils;
|
||||||
|
|
||||||
namespace ShardingCore.EFCores
|
namespace ShardingCore.EFCores
|
||||||
{
|
{
|
||||||
|
@ -37,6 +43,40 @@ namespace ShardingCore.EFCores
|
||||||
_context = (IShardingDbContext)context;
|
_context = (IShardingDbContext)context;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
private IVirtualDataSource _virtualDataSource;
|
||||||
|
|
||||||
|
protected IVirtualDataSource VirtualDataSource
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (null == _virtualDataSource)
|
||||||
|
{
|
||||||
|
_virtualDataSource =
|
||||||
|
(IVirtualDataSource) ShardingContainer.GetService(
|
||||||
|
typeof(IVirtualDataSource<>).GetGenericType0(_context.GetType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return _virtualDataSource;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private IVirtualTableManager _virtualTableManager;
|
||||||
|
|
||||||
|
protected IVirtualTableManager VirtualTableManager
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (null == _virtualTableManager)
|
||||||
|
{
|
||||||
|
_virtualTableManager =
|
||||||
|
(IVirtualTableManager) ShardingContainer.GetService(
|
||||||
|
typeof(IVirtualTableManager<>).GetGenericType0(_context.GetType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return _virtualTableManager;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
|
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
|
||||||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
|
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
|
||||||
|
@ -366,6 +406,89 @@ namespace ShardingCore.EFCores
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override TEntity Find(params object[] keyValues)
|
||||||
|
{
|
||||||
|
var primaryKeyFindDbContext = GetDbContextByKeyValue(keyValues);
|
||||||
|
if (primaryKeyFindDbContext != null)
|
||||||
|
{
|
||||||
|
return primaryKeyFindDbContext.Set<TEntity>().Find(keyValues);
|
||||||
|
}
|
||||||
|
return base.Find(keyValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !EFCORE2
|
||||||
|
public override ValueTask<TEntity> FindAsync(params object[] keyValues)
|
||||||
|
{
|
||||||
|
var primaryKeyFindDbContext = GetDbContextByKeyValue(keyValues);
|
||||||
|
if (primaryKeyFindDbContext != null)
|
||||||
|
{
|
||||||
|
return primaryKeyFindDbContext.Set<TEntity>().FindAsync(keyValues);
|
||||||
|
}
|
||||||
|
return base.FindAsync(keyValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override ValueTask<TEntity> FindAsync(object[] keyValues, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var primaryKeyFindDbContext = GetDbContextByKeyValue(keyValues);
|
||||||
|
if (primaryKeyFindDbContext != null)
|
||||||
|
{
|
||||||
|
return primaryKeyFindDbContext.Set<TEntity>().FindAsync(keyValues, cancellationToken);
|
||||||
|
}
|
||||||
|
return base.FindAsync(keyValues, cancellationToken);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#if EFCORE2
|
||||||
|
public override Task<TEntity> FindAsync(params object[] keyValues)
|
||||||
|
{
|
||||||
|
var primaryKeyFindDbContext = GetDbContextByKeyValue(keyValues);
|
||||||
|
if (primaryKeyFindDbContext != null)
|
||||||
|
{
|
||||||
|
return primaryKeyFindDbContext.Set<TEntity>().FindAsync(keyValues);
|
||||||
|
}
|
||||||
|
return base.FindAsync(keyValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Task<TEntity> FindAsync(object[] keyValues, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var primaryKeyFindDbContext = GetDbContextByKeyValue(keyValues);
|
||||||
|
if (primaryKeyFindDbContext != null)
|
||||||
|
{
|
||||||
|
return primaryKeyFindDbContext.Set<TEntity>().FindAsync(keyValues, cancellationToken);
|
||||||
|
}
|
||||||
|
return base.FindAsync(keyValues, cancellationToken);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
private DbContext GetDbContextByKeyValue(params object[] keyValues)
|
||||||
|
{
|
||||||
|
if (keyValues.Length == 1)
|
||||||
|
{
|
||||||
|
var shardingEntityConfig = ShardingUtil.Parse(typeof(TEntity));
|
||||||
|
//单key字段
|
||||||
|
if (null != shardingEntityConfig.SinglePrimaryKeyFieldName)
|
||||||
|
{
|
||||||
|
var isShardingDataSource = typeof(TEntity).IsShardingDataSource();
|
||||||
|
var shardingDataSourceFieldIsKey = shardingEntityConfig.ShardingDataSourceFieldIsKey();
|
||||||
|
if (isShardingDataSource && !shardingDataSourceFieldIsKey)
|
||||||
|
return null;
|
||||||
|
var isShardingTable = typeof(TEntity).IsShardingTable();
|
||||||
|
var shardingTableFieldIsKey = shardingEntityConfig.ShardingTableFieldIsKey();
|
||||||
|
if (isShardingTable && !shardingTableFieldIsKey)
|
||||||
|
return null;
|
||||||
|
var primaryKeyValue = keyValues[0];
|
||||||
|
if (primaryKeyValue != null)
|
||||||
|
{
|
||||||
|
var dataSourceName = VirtualDataSource.GetDataSourceName<TEntity>(primaryKeyValue);
|
||||||
|
var tableTail = VirtualTableManager.GetTableTail<TEntity>(primaryKeyValue);
|
||||||
|
var routeTail = ShardingContainer.GetService<IRouteTailFactory>().Create(tableTail);
|
||||||
|
return _context.GetDbContext(dataSourceName, false, routeTail);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using ShardingCore.Core.VirtualDatabase;
|
||||||
|
|
||||||
|
namespace ShardingCore.Extensions
|
||||||
|
{
|
||||||
|
public static class ShardingEntityConfigExtension
|
||||||
|
{
|
||||||
|
public static bool ShardingDataSourceFieldIsKey(this ShardingEntityConfig shardingEntityConfig)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(shardingEntityConfig.ShardingDataSourceField))
|
||||||
|
return false;
|
||||||
|
return shardingEntityConfig.ShardingDataSourceField == shardingEntityConfig.SinglePrimaryKeyFieldName;
|
||||||
|
}
|
||||||
|
public static bool ShardingTableFieldIsKey(this ShardingEntityConfig shardingEntityConfig)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(shardingEntityConfig.ShardingTableField))
|
||||||
|
return false;
|
||||||
|
return shardingEntityConfig.ShardingTableField == shardingEntityConfig.SinglePrimaryKeyFieldName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -79,6 +79,14 @@ namespace ShardingCore.Extensions
|
||||||
var physicTable = virtualTableManager.GetVirtualTable(entity.GetType()).RouteTo(new ShardingTableRouteConfig(null, entity as IShardingTable, null))[0];
|
var physicTable = virtualTableManager.GetVirtualTable(entity.GetType()).RouteTo(new ShardingTableRouteConfig(null, entity as IShardingTable, null))[0];
|
||||||
return physicTable.Tail;
|
return physicTable.Tail;
|
||||||
}
|
}
|
||||||
|
public static string GetTableTail<TEntity>(this IVirtualTableManager virtualTableManager,
|
||||||
|
object shardingKeyValue) where TEntity : class
|
||||||
|
{
|
||||||
|
if (!typeof(TEntity).IsShardingTable())
|
||||||
|
return string.Empty;
|
||||||
|
var physicTable = virtualTableManager.GetVirtualTable(typeof(TEntity)).RouteTo(new ShardingTableRouteConfig(shardingKeyValue: shardingKeyValue))[0];
|
||||||
|
return physicTable.Tail;
|
||||||
|
}
|
||||||
public static bool IsVirtualDataSourceRoute(this Type routeType)
|
public static bool IsVirtualDataSourceRoute(this Type routeType)
|
||||||
{
|
{
|
||||||
if (routeType == null)
|
if (routeType == null)
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
using System;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using ShardingCore.Core;
|
||||||
|
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||||
|
using ShardingCore.Core.VirtualRoutes;
|
||||||
|
using ShardingCore.Sharding.Abstractions;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Text;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using ShardingCore.Core;
|
|
||||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
|
||||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
|
|
||||||
using ShardingCore.Core.VirtualRoutes;
|
|
||||||
using ShardingCore.Sharding.Abstractions;
|
|
||||||
|
|
||||||
namespace ShardingCore.Extensions
|
namespace ShardingCore.Extensions
|
||||||
{
|
{
|
||||||
|
@ -21,7 +19,7 @@ namespace ShardingCore.Extensions
|
||||||
*/
|
*/
|
||||||
public static class VirtualDataSourceExtension
|
public static class VirtualDataSourceExtension
|
||||||
{
|
{
|
||||||
public static string GetDataSourceName<TShardingDbContext,TEntity>(this IVirtualDataSource<TShardingDbContext> virtualDataSource,TEntity entity) where TShardingDbContext : DbContext, IShardingDbContext where TEntity : class
|
public static string GetDataSourceName<TEntity>(this IVirtualDataSource virtualDataSource,TEntity entity)where TEntity : class
|
||||||
{
|
{
|
||||||
if (!entity.IsShardingDataSource())
|
if (!entity.IsShardingDataSource())
|
||||||
return virtualDataSource.DefaultDataSourceName;
|
return virtualDataSource.DefaultDataSourceName;
|
||||||
|
@ -30,12 +28,19 @@ namespace ShardingCore.Extensions
|
||||||
new ShardingDataSourceRouteConfig(shardingDataSource: entity as IShardingDataSource))[0];
|
new ShardingDataSourceRouteConfig(shardingDataSource: entity as IShardingDataSource))[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<string> GetDataSourceNames<TShardingDbContext, TEntity>(this IVirtualDataSource<TShardingDbContext> virtualDataSource, Expression<Func<TEntity, bool>> where)
|
public static List<string> GetDataSourceNames<TEntity>(this IVirtualDataSource virtualDataSource, Expression<Func<TEntity, bool>> where)
|
||||||
where TShardingDbContext : DbContext, IShardingDbContext
|
|
||||||
where TEntity : class
|
where TEntity : class
|
||||||
{
|
{
|
||||||
return virtualDataSource.RouteTo(typeof(TEntity),new ShardingDataSourceRouteConfig(predicate: where))
|
return virtualDataSource.RouteTo(typeof(TEntity),new ShardingDataSourceRouteConfig(predicate: where))
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
public static string GetDataSourceName<TEntity>(this IVirtualDataSource virtualDataSource, object shardingKeyValue) where TEntity : class
|
||||||
|
{
|
||||||
|
if (!typeof(TEntity).IsShardingDataSource())
|
||||||
|
return virtualDataSource.DefaultDataSourceName;
|
||||||
|
|
||||||
|
return virtualDataSource.RouteTo(typeof(TEntity),
|
||||||
|
new ShardingDataSourceRouteConfig(shardingKeyValue:shardingKeyValue))[0];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,8 @@ namespace ShardingCore.Sharding
|
||||||
(IShardingDbContextExecutor)Activator.CreateInstance(
|
(IShardingDbContextExecutor)Activator.CreateInstance(
|
||||||
typeof(ShardingDbContextExecutor<>).GetGenericType0(this.GetType()),this);
|
typeof(ShardingDbContextExecutor<>).GetGenericType0(this.GetType()),this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IsExecutor = wrapOptionsExtension == null;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 读写分离优先级
|
/// 读写分离优先级
|
||||||
|
@ -60,11 +62,12 @@ namespace ShardingCore.Sharding
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否是真正的执行者
|
/// 是否是真正的执行者
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private bool isExecutor => _shardingDbContextExecutor == null;
|
public bool IsExecutor { get; }
|
||||||
|
|
||||||
|
|
||||||
//public void ShardingUpgrade()
|
//public void ShardingUpgrade()
|
||||||
//{
|
//{
|
||||||
// //isExecutor = true;
|
// //IsExecutor = true;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
public DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail)
|
public DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail)
|
||||||
|
@ -86,14 +89,14 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override EntityEntry Add(object entity)
|
public override EntityEntry Add(object entity)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
base.Add(entity);
|
base.Add(entity);
|
||||||
return CreateGenericDbContext(entity).Add(entity);
|
return CreateGenericDbContext(entity).Add(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override EntityEntry<TEntity> Add<TEntity>(TEntity entity)
|
public override EntityEntry<TEntity> Add<TEntity>(TEntity entity)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.Add(entity);
|
return base.Add(entity);
|
||||||
return CreateGenericDbContext(entity).Add(entity);
|
return CreateGenericDbContext(entity).Add(entity);
|
||||||
}
|
}
|
||||||
|
@ -104,14 +107,14 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override ValueTask<EntityEntry<TEntity>> AddAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = new CancellationToken())
|
public override ValueTask<EntityEntry<TEntity>> AddAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = new CancellationToken())
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.AddAsync(entity, cancellationToken);
|
return base.AddAsync(entity, cancellationToken);
|
||||||
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override ValueTask<EntityEntry> AddAsync(object entity, CancellationToken cancellationToken = new CancellationToken())
|
public override ValueTask<EntityEntry> AddAsync(object entity, CancellationToken cancellationToken = new CancellationToken())
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.AddAsync(entity, cancellationToken);
|
return base.AddAsync(entity, cancellationToken);
|
||||||
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
||||||
}
|
}
|
||||||
|
@ -119,14 +122,14 @@ namespace ShardingCore.Sharding
|
||||||
#if EFCORE2
|
#if EFCORE2
|
||||||
public override Task<EntityEntry<TEntity>> AddAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = new CancellationToken())
|
public override Task<EntityEntry<TEntity>> AddAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = new CancellationToken())
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.AddAsync(entity, cancellationToken);
|
return base.AddAsync(entity, cancellationToken);
|
||||||
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<EntityEntry> AddAsync(object entity, CancellationToken cancellationToken = new CancellationToken())
|
public override Task<EntityEntry> AddAsync(object entity, CancellationToken cancellationToken = new CancellationToken())
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.AddAsync(entity, cancellationToken);
|
return base.AddAsync(entity, cancellationToken);
|
||||||
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
|
||||||
}
|
}
|
||||||
|
@ -134,7 +137,7 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override void AddRange(params object[] entities)
|
public override void AddRange(params object[] entities)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
{
|
{
|
||||||
base.AddRange(entities);
|
base.AddRange(entities);
|
||||||
return;
|
return;
|
||||||
|
@ -157,7 +160,7 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override void AddRange(IEnumerable<object> entities)
|
public override void AddRange(IEnumerable<object> entities)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
{
|
{
|
||||||
base.AddRange(entities);
|
base.AddRange(entities);
|
||||||
return;
|
return;
|
||||||
|
@ -180,7 +183,7 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override async Task AddRangeAsync(params object[] entities)
|
public override async Task AddRangeAsync(params object[] entities)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
{
|
{
|
||||||
await base.AddRangeAsync(entities);
|
await base.AddRangeAsync(entities);
|
||||||
return;
|
return;
|
||||||
|
@ -203,7 +206,7 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override async Task AddRangeAsync(IEnumerable<object> entities, CancellationToken cancellationToken = new CancellationToken())
|
public override async Task AddRangeAsync(IEnumerable<object> entities, CancellationToken cancellationToken = new CancellationToken())
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
{
|
{
|
||||||
await base.AddRangeAsync(entities, cancellationToken);
|
await base.AddRangeAsync(entities, cancellationToken);
|
||||||
return;
|
return;
|
||||||
|
@ -226,21 +229,21 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override EntityEntry<TEntity> Attach<TEntity>(TEntity entity)
|
public override EntityEntry<TEntity> Attach<TEntity>(TEntity entity)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.Attach(entity);
|
return base.Attach(entity);
|
||||||
return CreateGenericDbContext(entity).Attach(entity);
|
return CreateGenericDbContext(entity).Attach(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override EntityEntry Attach(object entity)
|
public override EntityEntry Attach(object entity)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.Attach(entity);
|
return base.Attach(entity);
|
||||||
return CreateGenericDbContext(entity).Attach(entity);
|
return CreateGenericDbContext(entity).Attach(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void AttachRange(params object[] entities)
|
public override void AttachRange(params object[] entities)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
{
|
{
|
||||||
base.AttachRange(entities);
|
base.AttachRange(entities);
|
||||||
return;
|
return;
|
||||||
|
@ -263,7 +266,7 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override void AttachRange(IEnumerable<object> entities)
|
public override void AttachRange(IEnumerable<object> entities)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
{
|
{
|
||||||
base.AttachRange(entities);
|
base.AttachRange(entities);
|
||||||
return;
|
return;
|
||||||
|
@ -291,35 +294,35 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override EntityEntry<TEntity> Entry<TEntity>(TEntity entity)
|
public override EntityEntry<TEntity> Entry<TEntity>(TEntity entity)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.Entry(entity);
|
return base.Entry(entity);
|
||||||
return CreateGenericDbContext(entity).Entry(entity);
|
return CreateGenericDbContext(entity).Entry(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override EntityEntry Entry(object entity)
|
public override EntityEntry Entry(object entity)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.Entry(entity);
|
return base.Entry(entity);
|
||||||
return CreateGenericDbContext(entity).Entry(entity);
|
return CreateGenericDbContext(entity).Entry(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override EntityEntry<TEntity> Update<TEntity>(TEntity entity)
|
public override EntityEntry<TEntity> Update<TEntity>(TEntity entity)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.Update(entity);
|
return base.Update(entity);
|
||||||
return CreateGenericDbContext(entity).Update(entity);
|
return CreateGenericDbContext(entity).Update(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override EntityEntry Update(object entity)
|
public override EntityEntry Update(object entity)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.Update(entity);
|
return base.Update(entity);
|
||||||
return CreateGenericDbContext(entity).Update(entity);
|
return CreateGenericDbContext(entity).Update(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void UpdateRange(params object[] entities)
|
public override void UpdateRange(params object[] entities)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
{
|
{
|
||||||
base.UpdateRange(entities);
|
base.UpdateRange(entities);
|
||||||
return;
|
return;
|
||||||
|
@ -342,7 +345,7 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override void UpdateRange(IEnumerable<object> entities)
|
public override void UpdateRange(IEnumerable<object> entities)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
{
|
{
|
||||||
base.UpdateRange(entities);
|
base.UpdateRange(entities);
|
||||||
return;
|
return;
|
||||||
|
@ -365,21 +368,21 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override EntityEntry<TEntity> Remove<TEntity>(TEntity entity)
|
public override EntityEntry<TEntity> Remove<TEntity>(TEntity entity)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.Remove(entity);
|
return base.Remove(entity);
|
||||||
return CreateGenericDbContext(entity).Remove(entity);
|
return CreateGenericDbContext(entity).Remove(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override EntityEntry Remove(object entity)
|
public override EntityEntry Remove(object entity)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.Remove(entity);
|
return base.Remove(entity);
|
||||||
return CreateGenericDbContext(entity).Remove(entity);
|
return CreateGenericDbContext(entity).Remove(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RemoveRange(params object[] entities)
|
public override void RemoveRange(params object[] entities)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
{
|
{
|
||||||
base.RemoveRange(entities);
|
base.RemoveRange(entities);
|
||||||
return;
|
return;
|
||||||
|
@ -402,7 +405,7 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override void RemoveRange(IEnumerable<object> entities)
|
public override void RemoveRange(IEnumerable<object> entities)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
{
|
{
|
||||||
base.RemoveRange(entities);
|
base.RemoveRange(entities);
|
||||||
return;
|
return;
|
||||||
|
@ -456,14 +459,14 @@ namespace ShardingCore.Sharding
|
||||||
public override int SaveChanges()
|
public override int SaveChanges()
|
||||||
{
|
{
|
||||||
|
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.SaveChanges();
|
return base.SaveChanges();
|
||||||
return this.SaveChanges(true);
|
return this.SaveChanges(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int SaveChanges(bool acceptAllChangesOnSuccess)
|
public override int SaveChanges(bool acceptAllChangesOnSuccess)
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.SaveChanges(acceptAllChangesOnSuccess);
|
return base.SaveChanges(acceptAllChangesOnSuccess);
|
||||||
//ApplyShardingConcepts();
|
//ApplyShardingConcepts();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
@ -487,14 +490,14 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
|
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return base.SaveChangesAsync(cancellationToken);
|
return base.SaveChangesAsync(cancellationToken);
|
||||||
return this.SaveChangesAsync(true, cancellationToken);
|
return this.SaveChangesAsync(true, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken())
|
public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken())
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
return await base.SaveChangesAsync(acceptAllChangesOnSuccess,cancellationToken);
|
return await base.SaveChangesAsync(acceptAllChangesOnSuccess,cancellationToken);
|
||||||
//ApplyShardingConcepts();
|
//ApplyShardingConcepts();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
@ -524,7 +527,7 @@ namespace ShardingCore.Sharding
|
||||||
public override void Dispose()
|
public override void Dispose()
|
||||||
{
|
{
|
||||||
|
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
{
|
{
|
||||||
base.Dispose();
|
base.Dispose();
|
||||||
}
|
}
|
||||||
|
@ -538,7 +541,7 @@ namespace ShardingCore.Sharding
|
||||||
|
|
||||||
public override async ValueTask DisposeAsync()
|
public override async ValueTask DisposeAsync()
|
||||||
{
|
{
|
||||||
if (isExecutor)
|
if (IsExecutor)
|
||||||
{
|
{
|
||||||
await base.DisposeAsync();
|
await base.DisposeAsync();
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ namespace ShardingCore.Sharding.Abstractions
|
||||||
/// <param name="routeTail"></param>
|
/// <param name="routeTail"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail);
|
DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建通用的db context
|
/// 创建通用的db context
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -28,5 +29,6 @@ namespace ShardingCore.Sharding.Abstractions
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
DbContext CreateGenericDbContext<T>(T entity) where T : class;
|
DbContext CreateGenericDbContext<T>(T entity) where T : class;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -98,6 +98,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
||||||
return dbContext;
|
return dbContext;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private DbContextOptions<TShardingDbContext> CreateParallelDbContextOptions(string dataSourceName)
|
private DbContextOptions<TShardingDbContext> CreateParallelDbContextOptions(string dataSourceName)
|
||||||
{
|
{
|
||||||
var dbContextOptionBuilder = DataSourceDbContext<TShardingDbContext>.CreateDbContextOptionBuilder();
|
var dbContextOptionBuilder = DataSourceDbContext<TShardingDbContext>.CreateDbContextOptionBuilder();
|
||||||
|
|
|
@ -78,8 +78,13 @@ namespace ShardingCore
|
||||||
EnsureCreated(context, dataSourceName);
|
EnsureCreated(context, dataSourceName);
|
||||||
foreach (var entity in context.Model.GetEntityTypes())
|
foreach (var entity in context.Model.GetEntityTypes())
|
||||||
{
|
{
|
||||||
//ShardingKeyUtil.ParsePrimaryKeyName(entity);
|
var entityType = entity.ClrType;
|
||||||
var entityType = entity.ClrType;
|
var primaryKeyNames = ShardingKeyUtil.ParsePrimaryKeyName(entity);
|
||||||
|
if (primaryKeyNames.Properties.Count == 1)
|
||||||
|
{
|
||||||
|
var shardingEntityConfig = ShardingUtil.Parse(entityType);
|
||||||
|
shardingEntityConfig.SinglePrimaryKeyFieldName = primaryKeyNames.Properties.First().Name;
|
||||||
|
}
|
||||||
//添加追踪模型
|
//添加追踪模型
|
||||||
_trackerManager.AddDbContextModel(entityType);
|
_trackerManager.AddDbContextModel(entityType);
|
||||||
if (entityType.IsShardingDataSource())
|
if (entityType.IsShardingDataSource())
|
||||||
|
|
|
@ -28,10 +28,11 @@ namespace ShardingCore.Utils
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ParsePrimaryKeyName(IEntityType entityType)
|
public static IKey ParsePrimaryKeyName(IEntityType entityType)
|
||||||
{
|
{
|
||||||
var primaryKey = entityType.FindPrimaryKey();
|
var primaryKey = entityType.FindPrimaryKey();
|
||||||
_caches.TryAdd(entityType.ClrType, primaryKey);
|
_caches.TryAdd(entityType.ClrType, primaryKey);
|
||||||
|
return primaryKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<object> GetPrimaryKeyValues(object entity)
|
public static IEnumerable<object> GetPrimaryKeyValues(object entity)
|
||||||
|
@ -54,6 +55,15 @@ namespace ShardingCore.Utils
|
||||||
|
|
||||||
return primaryKey;
|
return primaryKey;
|
||||||
}
|
}
|
||||||
|
public static IKey GetEntityIKey(Type entityType)
|
||||||
|
{
|
||||||
|
if (!_caches.TryGetValue(entityType, out var primaryKey))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return primaryKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -255,6 +255,8 @@ namespace ShardingCore.Test50
|
||||||
{
|
{
|
||||||
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefaultAsync();
|
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefaultAsync();
|
||||||
Assert.NotNull(sysUserMod);
|
Assert.NotNull(sysUserMod);
|
||||||
|
var userMod = _virtualDbContext.Set<SysUserMod>().Find("1");
|
||||||
|
Assert.Equal(sysUserMod, userMod);
|
||||||
Assert.True(sysUserMod.Id == "1");
|
Assert.True(sysUserMod.Id == "1");
|
||||||
var user198 = await _virtualDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "198");
|
var user198 = await _virtualDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "198");
|
||||||
Assert.True(user198.Id == "198");
|
Assert.True(user198.Id == "198");
|
||||||
|
@ -268,6 +270,7 @@ namespace ShardingCore.Test50
|
||||||
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
|
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
|
||||||
Assert.NotNull(sysUserMod);
|
Assert.NotNull(sysUserMod);
|
||||||
Assert.Equal("2", sysUserMod.Id);
|
Assert.Equal("2", sysUserMod.Id);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
@ -54,6 +54,7 @@ namespace ShardingCore.Test50
|
||||||
{
|
{
|
||||||
o.CreateShardingTableOnStart = true;
|
o.CreateShardingTableOnStart = true;
|
||||||
o.EnsureCreatedWithOutShardingTable = true;
|
o.EnsureCreatedWithOutShardingTable = true;
|
||||||
|
o.AutoTrackEntity = true;
|
||||||
})
|
})
|
||||||
.AddShardingTransaction((connection, builder) =>
|
.AddShardingTransaction((connection, builder) =>
|
||||||
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
|
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
|
||||||
|
|
|
@ -19,7 +19,7 @@ namespace ShardingCore.Test50_2x
|
||||||
private readonly ShardingDefaultDbContext _virtualDbContext;
|
private readonly ShardingDefaultDbContext _virtualDbContext;
|
||||||
private readonly IShardingRouteManager _shardingRouteManager;
|
private readonly IShardingRouteManager _shardingRouteManager;
|
||||||
|
|
||||||
public ShardingTest(ShardingDefaultDbContext virtualDbContext,IShardingRouteManager shardingRouteManager)
|
public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRouteManager shardingRouteManager)
|
||||||
{
|
{
|
||||||
_virtualDbContext = virtualDbContext;
|
_virtualDbContext = virtualDbContext;
|
||||||
_shardingRouteManager = shardingRouteManager;
|
_shardingRouteManager = shardingRouteManager;
|
||||||
|
@ -29,20 +29,19 @@ namespace ShardingCore.Test50_2x
|
||||||
//public async Task Route_TEST()
|
//public async Task Route_TEST()
|
||||||
//{
|
//{
|
||||||
// var queryable1 = _virtualDbContext.Set<SysUserMod>().Where(o=>o.Id=="339");
|
// var queryable1 = _virtualDbContext.Set<SysUserMod>().Where(o=>o.Id=="339");
|
||||||
// var routeResults1 = _routingRuleEngineFactory.Route(queryable1);
|
// var routeResults1 = _tableRouteRuleEngineFactory.Route(queryable1);
|
||||||
// Assert.Equal(1,routeResults1.Count());
|
// Assert.Equal(1,routeResults1.Count());
|
||||||
// Assert.Equal(1,routeResults1.FirstOrDefault().ReplaceTables.Count());
|
// Assert.Equal(1,routeResults1.FirstOrDefault().ReplaceTables.Count());
|
||||||
// Assert.Equal("0",routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().Tail);
|
// Assert.Equal("0",routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().Tail);
|
||||||
// Assert.Equal(nameof(SysUserMod),routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().OriginalName);
|
// Assert.Equal(nameof(SysUserMod),routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().OriginalName);
|
||||||
// var ids = new[] {"339", "124","142"};
|
// var ids = new[] {"339", "124","142"};
|
||||||
// var queryable2= _virtualDbContext.Set<SysUserMod>().Where(o=>ids.Contains(o.Id));
|
// var queryable2= _virtualDbContext.Set<SysUserMod>().Where(o=>ids.Contains(o.Id));
|
||||||
// var routeResult2s = _routingRuleEngineFactory.Route(queryable2);
|
// var routeResult2s = _tableRouteRuleEngineFactory.Route(queryable2);
|
||||||
// Assert.Equal(2,routeResult2s.Count());
|
// Assert.Equal(2,routeResult2s.Count());
|
||||||
// Assert.Equal(1,routeResult2s.FirstOrDefault().ReplaceTables.Count());
|
// Assert.Equal(1,routeResult2s.FirstOrDefault().ReplaceTables.Count());
|
||||||
// Assert.Equal(2,routeResult2s.SelectMany(o=>o.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)));
|
// Assert.Equal(true,routeResult2s.SelectMany(o=>o.ReplaceTables).All(o=>new[]{"0","1"}.Contains(o.Tail)));
|
||||||
//}
|
//}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ToList_All_Route_Test()
|
public async Task ToList_All_Route_Test()
|
||||||
{
|
{
|
||||||
|
@ -73,7 +72,6 @@ namespace ShardingCore.Test50_2x
|
||||||
descAge--;
|
descAge--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ToList_All_Test()
|
public async Task ToList_All_Test()
|
||||||
{
|
{
|
||||||
|
@ -97,6 +95,8 @@ namespace ShardingCore.Test50_2x
|
||||||
descAge--;
|
descAge--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var pageResult = await _virtualDbContext.Set<SysUserMod>().Skip(10).Take(10).OrderByDescending(o => o.Age).ToListAsync();
|
var pageResult = await _virtualDbContext.Set<SysUserMod>().Skip(10).Take(10).OrderByDescending(o => o.Age).ToListAsync();
|
||||||
Assert.Equal(10, pageResult.Count);
|
Assert.Equal(10, pageResult.Count);
|
||||||
int pageDescAge = 990;
|
int pageDescAge = 990;
|
||||||
|
@ -121,7 +121,8 @@ namespace ShardingCore.Test50_2x
|
||||||
DateOfMonth = salary.DateOfMonth,
|
DateOfMonth = salary.DateOfMonth,
|
||||||
Name = u.Name
|
Name = u.Name
|
||||||
}).ToListAsync();
|
}).ToListAsync();
|
||||||
var list2 = list.OrderBy(o=>o.Age).Select(o=>o.Age).Distinct().ToList();
|
|
||||||
|
var list2 = list.OrderBy(o => o.Age).Select(o => o.Age).Distinct().ToList();
|
||||||
Assert.Equal(24000, list.Count());
|
Assert.Equal(24000, list.Count());
|
||||||
Assert.Equal(24, list.Count(o => o.Name == "name_200"));
|
Assert.Equal(24, list.Count(o => o.Name == "name_200"));
|
||||||
|
|
||||||
|
@ -165,7 +166,7 @@ namespace ShardingCore.Test50_2x
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ToList_Id_In_Test()
|
public async Task ToList_Id_In_Test()
|
||||||
{
|
{
|
||||||
var ids = new[] {"1", "2", "3", "4"};
|
var ids = new[] { "1", "2", "3", "4" };
|
||||||
var sysUserMods = await _virtualDbContext.Set<SysUserMod>().Where(o => ids.Contains(o.Id)).ToListAsync();
|
var sysUserMods = await _virtualDbContext.Set<SysUserMod>().Where(o => ids.Contains(o.Id)).ToListAsync();
|
||||||
foreach (var id in ids)
|
foreach (var id in ids)
|
||||||
{
|
{
|
||||||
|
@ -241,6 +242,7 @@ namespace ShardingCore.Test50_2x
|
||||||
|
|
||||||
var sysUserModDesc = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Id).FirstOrDefaultAsync();
|
var sysUserModDesc = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Id).FirstOrDefaultAsync();
|
||||||
Assert.True(sysUserModDesc != null && sysUserModDesc.Id == "999");
|
Assert.True(sysUserModDesc != null && sysUserModDesc.Id == "999");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
@ -248,6 +250,8 @@ namespace ShardingCore.Test50_2x
|
||||||
{
|
{
|
||||||
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefaultAsync();
|
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefaultAsync();
|
||||||
Assert.NotNull(sysUserMod);
|
Assert.NotNull(sysUserMod);
|
||||||
|
var userMod = _virtualDbContext.Set<SysUserMod>().Find("1");
|
||||||
|
Assert.Equal(sysUserMod, userMod);
|
||||||
Assert.True(sysUserMod.Id == "1");
|
Assert.True(sysUserMod.Id == "1");
|
||||||
var user198 = await _virtualDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "198");
|
var user198 = await _virtualDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "198");
|
||||||
Assert.True(user198.Id == "198");
|
Assert.True(user198.Id == "198");
|
||||||
|
@ -261,6 +265,7 @@ namespace ShardingCore.Test50_2x
|
||||||
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
|
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
|
||||||
Assert.NotNull(sysUserMod);
|
Assert.NotNull(sysUserMod);
|
||||||
Assert.Equal("2", sysUserMod.Id);
|
Assert.Equal("2", sysUserMod.Id);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
@ -319,14 +324,14 @@ namespace ShardingCore.Test50_2x
|
||||||
public async Task Max_Join_Test()
|
public async Task Max_Join_Test()
|
||||||
{
|
{
|
||||||
var queryable = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
|
var queryable = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
|
||||||
join salary in _virtualDbContext.Set<SysUserSalary>()
|
join salary in _virtualDbContext.Set<SysUserSalary>()
|
||||||
on u.Id equals salary.UserId
|
on u.Id equals salary.UserId
|
||||||
select new
|
select new
|
||||||
{
|
{
|
||||||
Salary = salary.Salary,
|
Salary = salary.Salary,
|
||||||
DateOfMonth = salary.DateOfMonth,
|
DateOfMonth = salary.DateOfMonth,
|
||||||
Name = u.Name
|
Name = u.Name
|
||||||
});
|
});
|
||||||
var maxSalary = await queryable.MaxAsync(o => o.Salary);
|
var maxSalary = await queryable.MaxAsync(o => o.Salary);
|
||||||
Assert.Equal(1390000, maxSalary);
|
Assert.Equal(1390000, maxSalary);
|
||||||
}
|
}
|
||||||
|
@ -360,25 +365,25 @@ namespace ShardingCore.Test50_2x
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Group_Test()
|
public async Task Group_Test()
|
||||||
{
|
{
|
||||||
var ids = new[] {"200", "300"};
|
var ids = new[] { "200", "300" };
|
||||||
var dateOfMonths = new[] {202111, 202110};
|
var dateOfMonths = new[] { 202111, 202110 };
|
||||||
var group = await (from u in _virtualDbContext.Set<SysUserSalary>()
|
var group = await (from u in _virtualDbContext.Set<SysUserSalary>()
|
||||||
.Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
|
.Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
|
||||||
group u by new
|
group u by new
|
||||||
{
|
{
|
||||||
UId = u.UserId
|
UId = u.UserId
|
||||||
}
|
}
|
||||||
into g
|
into g
|
||||||
select new
|
select new
|
||||||
{
|
{
|
||||||
GroupUserId = g.Key.UId,
|
GroupUserId = g.Key.UId,
|
||||||
Count = g.Count(),
|
Count = g.Count(),
|
||||||
TotalSalary = g.Sum(o => o.Salary),
|
TotalSalary = g.Sum(o => o.Salary),
|
||||||
AvgSalary = g.Average(o => o.Salary),
|
AvgSalary = g.Average(o => o.Salary),
|
||||||
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
|
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
|
||||||
MinSalary = g.Min(o => o.Salary),
|
MinSalary = g.Min(o => o.Salary),
|
||||||
MaxSalary = g.Max(o => o.Salary)
|
MaxSalary = g.Max(o => o.Salary)
|
||||||
}).ToListAsync();
|
}).ToListAsync();
|
||||||
Assert.Equal(2, group.Count);
|
Assert.Equal(2, group.Count);
|
||||||
Assert.Equal(2, group[0].Count);
|
Assert.Equal(2, group[0].Count);
|
||||||
Assert.Equal(2260000, group[0].TotalSalary);
|
Assert.Equal(2260000, group[0].TotalSalary);
|
||||||
|
|
|
@ -51,6 +51,7 @@ namespace ShardingCore.Test50_2x
|
||||||
{
|
{
|
||||||
o.CreateShardingTableOnStart = true;
|
o.CreateShardingTableOnStart = true;
|
||||||
o.EnsureCreatedWithOutShardingTable = true;
|
o.EnsureCreatedWithOutShardingTable = true;
|
||||||
|
o.AutoTrackEntity = true;
|
||||||
})
|
})
|
||||||
.AddShardingTransaction((connection, builder) =>
|
.AddShardingTransaction((connection, builder) =>
|
||||||
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
|
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
|
||||||
|
|
|
@ -29,20 +29,19 @@ namespace ShardingCore.Test50_3x
|
||||||
//public async Task Route_TEST()
|
//public async Task Route_TEST()
|
||||||
//{
|
//{
|
||||||
// var queryable1 = _virtualDbContext.Set<SysUserMod>().Where(o=>o.Id=="339");
|
// var queryable1 = _virtualDbContext.Set<SysUserMod>().Where(o=>o.Id=="339");
|
||||||
// var routeResults1 = _routingRuleEngineFactory.Route(queryable1);
|
// var routeResults1 = _tableRouteRuleEngineFactory.Route(queryable1);
|
||||||
// Assert.Equal(1,routeResults1.Count());
|
// Assert.Equal(1,routeResults1.Count());
|
||||||
// Assert.Equal(1,routeResults1.FirstOrDefault().ReplaceTables.Count());
|
// Assert.Equal(1,routeResults1.FirstOrDefault().ReplaceTables.Count());
|
||||||
// Assert.Equal("0",routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().Tail);
|
// Assert.Equal("0",routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().Tail);
|
||||||
// Assert.Equal(nameof(SysUserMod),routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().OriginalName);
|
// Assert.Equal(nameof(SysUserMod),routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().OriginalName);
|
||||||
// var ids = new[] {"339", "124","142"};
|
// var ids = new[] {"339", "124","142"};
|
||||||
// var queryable2= _virtualDbContext.Set<SysUserMod>().Where(o=>ids.Contains(o.Id));
|
// var queryable2= _virtualDbContext.Set<SysUserMod>().Where(o=>ids.Contains(o.Id));
|
||||||
// var routeResult2s = _routingRuleEngineFactory.Route(queryable2);
|
// var routeResult2s = _tableRouteRuleEngineFactory.Route(queryable2);
|
||||||
// Assert.Equal(2,routeResult2s.Count());
|
// Assert.Equal(2,routeResult2s.Count());
|
||||||
// Assert.Equal(1,routeResult2s.FirstOrDefault().ReplaceTables.Count());
|
// Assert.Equal(1,routeResult2s.FirstOrDefault().ReplaceTables.Count());
|
||||||
// Assert.Equal(2,routeResult2s.SelectMany(o=>o.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)));
|
// Assert.Equal(true,routeResult2s.SelectMany(o=>o.ReplaceTables).All(o=>new[]{"0","1"}.Contains(o.Tail)));
|
||||||
//}
|
//}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ToList_All_Route_Test()
|
public async Task ToList_All_Route_Test()
|
||||||
{
|
{
|
||||||
|
@ -73,7 +72,6 @@ namespace ShardingCore.Test50_3x
|
||||||
descAge--;
|
descAge--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ToList_All_Test()
|
public async Task ToList_All_Test()
|
||||||
{
|
{
|
||||||
|
@ -97,6 +95,8 @@ namespace ShardingCore.Test50_3x
|
||||||
descAge--;
|
descAge--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var pageResult = await _virtualDbContext.Set<SysUserMod>().Skip(10).Take(10).OrderByDescending(o => o.Age).ToListAsync();
|
var pageResult = await _virtualDbContext.Set<SysUserMod>().Skip(10).Take(10).OrderByDescending(o => o.Age).ToListAsync();
|
||||||
Assert.Equal(10, pageResult.Count);
|
Assert.Equal(10, pageResult.Count);
|
||||||
int pageDescAge = 990;
|
int pageDescAge = 990;
|
||||||
|
@ -121,7 +121,8 @@ namespace ShardingCore.Test50_3x
|
||||||
DateOfMonth = salary.DateOfMonth,
|
DateOfMonth = salary.DateOfMonth,
|
||||||
Name = u.Name
|
Name = u.Name
|
||||||
}).ToListAsync();
|
}).ToListAsync();
|
||||||
var list2 = list.OrderBy(o=>o.Age).Select(o=>o.Age).Distinct().ToList();
|
|
||||||
|
var list2 = list.OrderBy(o => o.Age).Select(o => o.Age).Distinct().ToList();
|
||||||
Assert.Equal(24000, list.Count());
|
Assert.Equal(24000, list.Count());
|
||||||
Assert.Equal(24, list.Count(o => o.Name == "name_200"));
|
Assert.Equal(24, list.Count(o => o.Name == "name_200"));
|
||||||
|
|
||||||
|
@ -165,7 +166,7 @@ namespace ShardingCore.Test50_3x
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ToList_Id_In_Test()
|
public async Task ToList_Id_In_Test()
|
||||||
{
|
{
|
||||||
var ids = new[] {"1", "2", "3", "4"};
|
var ids = new[] { "1", "2", "3", "4" };
|
||||||
var sysUserMods = await _virtualDbContext.Set<SysUserMod>().Where(o => ids.Contains(o.Id)).ToListAsync();
|
var sysUserMods = await _virtualDbContext.Set<SysUserMod>().Where(o => ids.Contains(o.Id)).ToListAsync();
|
||||||
foreach (var id in ids)
|
foreach (var id in ids)
|
||||||
{
|
{
|
||||||
|
@ -241,6 +242,7 @@ namespace ShardingCore.Test50_3x
|
||||||
|
|
||||||
var sysUserModDesc = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Id).FirstOrDefaultAsync();
|
var sysUserModDesc = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Id).FirstOrDefaultAsync();
|
||||||
Assert.True(sysUserModDesc != null && sysUserModDesc.Id == "999");
|
Assert.True(sysUserModDesc != null && sysUserModDesc.Id == "999");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
@ -248,6 +250,8 @@ namespace ShardingCore.Test50_3x
|
||||||
{
|
{
|
||||||
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefaultAsync();
|
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefaultAsync();
|
||||||
Assert.NotNull(sysUserMod);
|
Assert.NotNull(sysUserMod);
|
||||||
|
var userMod = _virtualDbContext.Set<SysUserMod>().Find("1");
|
||||||
|
Assert.Equal(sysUserMod, userMod);
|
||||||
Assert.True(sysUserMod.Id == "1");
|
Assert.True(sysUserMod.Id == "1");
|
||||||
var user198 = await _virtualDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "198");
|
var user198 = await _virtualDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "198");
|
||||||
Assert.True(user198.Id == "198");
|
Assert.True(user198.Id == "198");
|
||||||
|
@ -261,6 +265,7 @@ namespace ShardingCore.Test50_3x
|
||||||
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
|
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
|
||||||
Assert.NotNull(sysUserMod);
|
Assert.NotNull(sysUserMod);
|
||||||
Assert.Equal("2", sysUserMod.Id);
|
Assert.Equal("2", sysUserMod.Id);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
@ -319,14 +324,14 @@ namespace ShardingCore.Test50_3x
|
||||||
public async Task Max_Join_Test()
|
public async Task Max_Join_Test()
|
||||||
{
|
{
|
||||||
var queryable = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
|
var queryable = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
|
||||||
join salary in _virtualDbContext.Set<SysUserSalary>()
|
join salary in _virtualDbContext.Set<SysUserSalary>()
|
||||||
on u.Id equals salary.UserId
|
on u.Id equals salary.UserId
|
||||||
select new
|
select new
|
||||||
{
|
{
|
||||||
Salary = salary.Salary,
|
Salary = salary.Salary,
|
||||||
DateOfMonth = salary.DateOfMonth,
|
DateOfMonth = salary.DateOfMonth,
|
||||||
Name = u.Name
|
Name = u.Name
|
||||||
});
|
});
|
||||||
var maxSalary = await queryable.MaxAsync(o => o.Salary);
|
var maxSalary = await queryable.MaxAsync(o => o.Salary);
|
||||||
Assert.Equal(1390000, maxSalary);
|
Assert.Equal(1390000, maxSalary);
|
||||||
}
|
}
|
||||||
|
@ -360,25 +365,25 @@ namespace ShardingCore.Test50_3x
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Group_Test()
|
public async Task Group_Test()
|
||||||
{
|
{
|
||||||
var ids = new[] {"200", "300"};
|
var ids = new[] { "200", "300" };
|
||||||
var dateOfMonths = new[] {202111, 202110};
|
var dateOfMonths = new[] { 202111, 202110 };
|
||||||
var group = await (from u in _virtualDbContext.Set<SysUserSalary>()
|
var group = await (from u in _virtualDbContext.Set<SysUserSalary>()
|
||||||
.Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
|
.Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
|
||||||
group u by new
|
group u by new
|
||||||
{
|
{
|
||||||
UId = u.UserId
|
UId = u.UserId
|
||||||
}
|
}
|
||||||
into g
|
into g
|
||||||
select new
|
select new
|
||||||
{
|
{
|
||||||
GroupUserId = g.Key.UId,
|
GroupUserId = g.Key.UId,
|
||||||
Count = g.Count(),
|
Count = g.Count(),
|
||||||
TotalSalary = g.Sum(o => o.Salary),
|
TotalSalary = g.Sum(o => o.Salary),
|
||||||
AvgSalary = g.Average(o => o.Salary),
|
AvgSalary = g.Average(o => o.Salary),
|
||||||
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
|
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
|
||||||
MinSalary = g.Min(o => o.Salary),
|
MinSalary = g.Min(o => o.Salary),
|
||||||
MaxSalary = g.Max(o => o.Salary)
|
MaxSalary = g.Max(o => o.Salary)
|
||||||
}).ToListAsync();
|
}).ToListAsync();
|
||||||
Assert.Equal(2, group.Count);
|
Assert.Equal(2, group.Count);
|
||||||
Assert.Equal(2, group[0].Count);
|
Assert.Equal(2, group[0].Count);
|
||||||
Assert.Equal(2260000, group[0].TotalSalary);
|
Assert.Equal(2260000, group[0].TotalSalary);
|
||||||
|
|
|
@ -47,6 +47,7 @@ namespace ShardingCore.Test50_3x
|
||||||
{
|
{
|
||||||
o.CreateShardingTableOnStart = true;
|
o.CreateShardingTableOnStart = true;
|
||||||
o.EnsureCreatedWithOutShardingTable = true;
|
o.EnsureCreatedWithOutShardingTable = true;
|
||||||
|
o.AutoTrackEntity = true;
|
||||||
})
|
})
|
||||||
.AddShardingTransaction((connection, builder) =>
|
.AddShardingTransaction((connection, builder) =>
|
||||||
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
|
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
|
||||||
|
|
Loading…
Reference in New Issue