发布.3.1.11并且修复find未能正确路由到dbcontext的bug

This commit is contained in:
xuejiaming 2021-10-26 15:06:20 +08:00
parent 7efb0697d9
commit 96c2833a49
19 changed files with 322 additions and 116 deletions

View File

@ -1,8 +1,8 @@
:start
::定义版本
set EFCORE2=2.3.1.10
set EFCORE3=3.3.1.10
set EFCORE5=5.3.1.10
set EFCORE2=2.3.1.11
set EFCORE3=3.3.1.11
set EFCORE5=5.3.1.11
::删除所有bin与obj下的文件
@echo off

View File

@ -60,7 +60,7 @@ namespace Samples.AbpSharding
//public void ShardingUpgrade()
//{
// //isExecutor = true;
// //IsExecutor = true;
//}
public DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail)

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace ShardingCore.Core.VirtualDatabase
@ -29,6 +30,11 @@ namespace ShardingCore.Core.VirtualDatabase
/// 分库字段
/// </summary>
public string ShardingDataSourceField { get; set; }
/// <summary>
/// 主键名称
/// </summary>
public string SinglePrimaryKeyFieldName { get; set; }
/// <summary>
/// 启动时是否建表 auto create data source when start app

View File

@ -15,11 +15,8 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
* @Date: Friday, 05 February 2021 13:01:39
* @Email: 326308290@qq.com
*/
/// <summary>
/// 虚拟数据源 连接所有的实际数据源
/// </summary>
public interface IVirtualDataSource<TShardingDbContext>
where TShardingDbContext : DbContext, IShardingDbContext
public interface IVirtualDataSource
{
string DefaultDataSourceName { get; }
/// <summary>
@ -55,4 +52,11 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
bool AddVirtualDataSourceRoute(IVirtualDataSourceRoute virtualDataSourceRoute);
bool IsDefault(string dataSourceName);
}
/// <summary>
/// 虚拟数据源 连接所有的实际数据源
/// </summary>
public interface IVirtualDataSource<TShardingDbContext> : IVirtualDataSource
where TShardingDbContext : DbContext, IShardingDbContext
{
}
}

View File

@ -8,6 +8,12 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
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
{
@ -37,6 +43,40 @@ namespace ShardingCore.EFCores
_context = (IShardingDbContext)context;
}
#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>
/// 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
@ -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;
}
}
}

View File

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

View File

@ -79,6 +79,14 @@ namespace ShardingCore.Extensions
var physicTable = virtualTableManager.GetVirtualTable(entity.GetType()).RouteTo(new ShardingTableRouteConfig(null, entity as IShardingTable, null))[0];
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)
{
if (routeType == null)

View File

@ -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.Linq;
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
{
@ -21,7 +19,7 @@ namespace ShardingCore.Extensions
*/
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())
return virtualDataSource.DefaultDataSourceName;
@ -30,12 +28,19 @@ namespace ShardingCore.Extensions
new ShardingDataSourceRouteConfig(shardingDataSource: entity as IShardingDataSource))[0];
}
public static List<string> GetDataSourceNames<TShardingDbContext, TEntity>(this IVirtualDataSource<TShardingDbContext> virtualDataSource, Expression<Func<TEntity, bool>> where)
where TShardingDbContext : DbContext, IShardingDbContext
public static List<string> GetDataSourceNames<TEntity>(this IVirtualDataSource virtualDataSource, Expression<Func<TEntity, bool>> where)
where TEntity : class
{
return virtualDataSource.RouteTo(typeof(TEntity),new ShardingDataSourceRouteConfig(predicate: where))
.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];
}
}
}

View File

@ -39,6 +39,8 @@ namespace ShardingCore.Sharding
(IShardingDbContextExecutor)Activator.CreateInstance(
typeof(ShardingDbContextExecutor<>).GetGenericType0(this.GetType()),this);
}
IsExecutor = wrapOptionsExtension == null;
}
/// <summary>
/// 读写分离优先级
@ -60,11 +62,12 @@ namespace ShardingCore.Sharding
/// <summary>
/// 是否是真正的执行者
/// </summary>
private bool isExecutor => _shardingDbContextExecutor == null;
public bool IsExecutor { get; }
//public void ShardingUpgrade()
//{
// //isExecutor = true;
// //IsExecutor = true;
//}
public DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail)
@ -86,14 +89,14 @@ namespace ShardingCore.Sharding
public override EntityEntry Add(object entity)
{
if (isExecutor)
if (IsExecutor)
base.Add(entity);
return CreateGenericDbContext(entity).Add(entity);
}
public override EntityEntry<TEntity> Add<TEntity>(TEntity entity)
{
if (isExecutor)
if (IsExecutor)
return base.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())
{
if (isExecutor)
if (IsExecutor)
return base.AddAsync(entity, cancellationToken);
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
}
public override ValueTask<EntityEntry> AddAsync(object entity, CancellationToken cancellationToken = new CancellationToken())
{
if (isExecutor)
if (IsExecutor)
return base.AddAsync(entity, cancellationToken);
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
}
@ -119,14 +122,14 @@ namespace ShardingCore.Sharding
#if EFCORE2
public override Task<EntityEntry<TEntity>> AddAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = new CancellationToken())
{
if (isExecutor)
if (IsExecutor)
return base.AddAsync(entity, cancellationToken);
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
}
public override Task<EntityEntry> AddAsync(object entity, CancellationToken cancellationToken = new CancellationToken())
{
if (isExecutor)
if (IsExecutor)
return base.AddAsync(entity, cancellationToken);
return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken);
}
@ -134,7 +137,7 @@ namespace ShardingCore.Sharding
public override void AddRange(params object[] entities)
{
if (isExecutor)
if (IsExecutor)
{
base.AddRange(entities);
return;
@ -157,7 +160,7 @@ namespace ShardingCore.Sharding
public override void AddRange(IEnumerable<object> entities)
{
if (isExecutor)
if (IsExecutor)
{
base.AddRange(entities);
return;
@ -180,7 +183,7 @@ namespace ShardingCore.Sharding
public override async Task AddRangeAsync(params object[] entities)
{
if (isExecutor)
if (IsExecutor)
{
await base.AddRangeAsync(entities);
return;
@ -203,7 +206,7 @@ namespace ShardingCore.Sharding
public override async Task AddRangeAsync(IEnumerable<object> entities, CancellationToken cancellationToken = new CancellationToken())
{
if (isExecutor)
if (IsExecutor)
{
await base.AddRangeAsync(entities, cancellationToken);
return;
@ -226,21 +229,21 @@ namespace ShardingCore.Sharding
public override EntityEntry<TEntity> Attach<TEntity>(TEntity entity)
{
if (isExecutor)
if (IsExecutor)
return base.Attach(entity);
return CreateGenericDbContext(entity).Attach(entity);
}
public override EntityEntry Attach(object entity)
{
if (isExecutor)
if (IsExecutor)
return base.Attach(entity);
return CreateGenericDbContext(entity).Attach(entity);
}
public override void AttachRange(params object[] entities)
{
if (isExecutor)
if (IsExecutor)
{
base.AttachRange(entities);
return;
@ -263,7 +266,7 @@ namespace ShardingCore.Sharding
public override void AttachRange(IEnumerable<object> entities)
{
if (isExecutor)
if (IsExecutor)
{
base.AttachRange(entities);
return;
@ -291,35 +294,35 @@ namespace ShardingCore.Sharding
public override EntityEntry<TEntity> Entry<TEntity>(TEntity entity)
{
if (isExecutor)
if (IsExecutor)
return base.Entry(entity);
return CreateGenericDbContext(entity).Entry(entity);
}
public override EntityEntry Entry(object entity)
{
if (isExecutor)
if (IsExecutor)
return base.Entry(entity);
return CreateGenericDbContext(entity).Entry(entity);
}
public override EntityEntry<TEntity> Update<TEntity>(TEntity entity)
{
if (isExecutor)
if (IsExecutor)
return base.Update(entity);
return CreateGenericDbContext(entity).Update(entity);
}
public override EntityEntry Update(object entity)
{
if (isExecutor)
if (IsExecutor)
return base.Update(entity);
return CreateGenericDbContext(entity).Update(entity);
}
public override void UpdateRange(params object[] entities)
{
if (isExecutor)
if (IsExecutor)
{
base.UpdateRange(entities);
return;
@ -342,7 +345,7 @@ namespace ShardingCore.Sharding
public override void UpdateRange(IEnumerable<object> entities)
{
if (isExecutor)
if (IsExecutor)
{
base.UpdateRange(entities);
return;
@ -365,21 +368,21 @@ namespace ShardingCore.Sharding
public override EntityEntry<TEntity> Remove<TEntity>(TEntity entity)
{
if (isExecutor)
if (IsExecutor)
return base.Remove(entity);
return CreateGenericDbContext(entity).Remove(entity);
}
public override EntityEntry Remove(object entity)
{
if (isExecutor)
if (IsExecutor)
return base.Remove(entity);
return CreateGenericDbContext(entity).Remove(entity);
}
public override void RemoveRange(params object[] entities)
{
if (isExecutor)
if (IsExecutor)
{
base.RemoveRange(entities);
return;
@ -402,7 +405,7 @@ namespace ShardingCore.Sharding
public override void RemoveRange(IEnumerable<object> entities)
{
if (isExecutor)
if (IsExecutor)
{
base.RemoveRange(entities);
return;
@ -456,14 +459,14 @@ namespace ShardingCore.Sharding
public override int SaveChanges()
{
if (isExecutor)
if (IsExecutor)
return base.SaveChanges();
return this.SaveChanges(true);
}
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
if (isExecutor)
if (IsExecutor)
return base.SaveChanges(acceptAllChangesOnSuccess);
//ApplyShardingConcepts();
int i = 0;
@ -487,14 +490,14 @@ namespace ShardingCore.Sharding
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
if (isExecutor)
if (IsExecutor)
return base.SaveChangesAsync(cancellationToken);
return this.SaveChangesAsync(true, cancellationToken);
}
public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken())
{
if (isExecutor)
if (IsExecutor)
return await base.SaveChangesAsync(acceptAllChangesOnSuccess,cancellationToken);
//ApplyShardingConcepts();
int i = 0;
@ -524,7 +527,7 @@ namespace ShardingCore.Sharding
public override void Dispose()
{
if (isExecutor)
if (IsExecutor)
{
base.Dispose();
}
@ -538,7 +541,7 @@ namespace ShardingCore.Sharding
public override async ValueTask DisposeAsync()
{
if (isExecutor)
if (IsExecutor)
{
await base.DisposeAsync();
}

View File

@ -20,6 +20,7 @@ namespace ShardingCore.Sharding.Abstractions
/// <param name="routeTail"></param>
/// <returns></returns>
DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail);
/// <summary>
/// 创建通用的db context
/// </summary>
@ -28,5 +29,6 @@ namespace ShardingCore.Sharding.Abstractions
/// <returns></returns>
DbContext CreateGenericDbContext<T>(T entity) where T : class;
}
}

View File

@ -98,6 +98,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
return dbContext;
}
}
private DbContextOptions<TShardingDbContext> CreateParallelDbContextOptions(string dataSourceName)
{
var dbContextOptionBuilder = DataSourceDbContext<TShardingDbContext>.CreateDbContextOptionBuilder();

View File

@ -78,8 +78,13 @@ namespace ShardingCore
EnsureCreated(context, dataSourceName);
foreach (var entity in context.Model.GetEntityTypes())
{
//ShardingKeyUtil.ParsePrimaryKeyName(entity);
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);
if (entityType.IsShardingDataSource())

View File

@ -28,10 +28,11 @@ namespace ShardingCore.Utils
{
}
public static void ParsePrimaryKeyName(IEntityType entityType)
public static IKey ParsePrimaryKeyName(IEntityType entityType)
{
var primaryKey = entityType.FindPrimaryKey();
_caches.TryAdd(entityType.ClrType, primaryKey);
return primaryKey;
}
public static IEnumerable<object> GetPrimaryKeyValues(object entity)
@ -54,6 +55,15 @@ namespace ShardingCore.Utils
return primaryKey;
}
public static IKey GetEntityIKey(Type entityType)
{
if (!_caches.TryGetValue(entityType, out var primaryKey))
{
return null;
}
return primaryKey;
}

View File

@ -255,6 +255,8 @@ namespace ShardingCore.Test50
{
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefaultAsync();
Assert.NotNull(sysUserMod);
var userMod = _virtualDbContext.Set<SysUserMod>().Find("1");
Assert.Equal(sysUserMod, userMod);
Assert.True(sysUserMod.Id == "1");
var user198 = await _virtualDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.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();
Assert.NotNull(sysUserMod);
Assert.Equal("2", sysUserMod.Id);
}
[Fact]

View File

@ -54,6 +54,7 @@ namespace ShardingCore.Test50
{
o.CreateShardingTableOnStart = true;
o.EnsureCreatedWithOutShardingTable = true;
o.AutoTrackEntity = true;
})
.AddShardingTransaction((connection, builder) =>
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))

View File

@ -29,20 +29,19 @@ namespace ShardingCore.Test50_2x
//public async Task Route_TEST()
//{
// 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.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);
// var routeResult2s = _tableRouteRuleEngineFactory.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()
{
@ -73,7 +72,6 @@ namespace ShardingCore.Test50_2x
descAge--;
}
}
[Fact]
public async Task ToList_All_Test()
{
@ -97,6 +95,8 @@ namespace ShardingCore.Test50_2x
descAge--;
}
var pageResult = await _virtualDbContext.Set<SysUserMod>().Skip(10).Take(10).OrderByDescending(o => o.Age).ToListAsync();
Assert.Equal(10, pageResult.Count);
int pageDescAge = 990;
@ -121,6 +121,7 @@ namespace ShardingCore.Test50_2x
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"));
@ -241,6 +242,7 @@ namespace ShardingCore.Test50_2x
var sysUserModDesc = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Id).FirstOrDefaultAsync();
Assert.True(sysUserModDesc != null && sysUserModDesc.Id == "999");
}
[Fact]
@ -248,6 +250,8 @@ namespace ShardingCore.Test50_2x
{
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefaultAsync();
Assert.NotNull(sysUserMod);
var userMod = _virtualDbContext.Set<SysUserMod>().Find("1");
Assert.Equal(sysUserMod, userMod);
Assert.True(sysUserMod.Id == "1");
var user198 = await _virtualDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.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();
Assert.NotNull(sysUserMod);
Assert.Equal("2", sysUserMod.Id);
}
[Fact]

View File

@ -51,6 +51,7 @@ namespace ShardingCore.Test50_2x
{
o.CreateShardingTableOnStart = true;
o.EnsureCreatedWithOutShardingTable = true;
o.AutoTrackEntity = true;
})
.AddShardingTransaction((connection, builder) =>
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))

View File

@ -29,20 +29,19 @@ namespace ShardingCore.Test50_3x
//public async Task Route_TEST()
//{
// 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.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);
// var routeResult2s = _tableRouteRuleEngineFactory.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()
{
@ -73,7 +72,6 @@ namespace ShardingCore.Test50_3x
descAge--;
}
}
[Fact]
public async Task ToList_All_Test()
{
@ -97,6 +95,8 @@ namespace ShardingCore.Test50_3x
descAge--;
}
var pageResult = await _virtualDbContext.Set<SysUserMod>().Skip(10).Take(10).OrderByDescending(o => o.Age).ToListAsync();
Assert.Equal(10, pageResult.Count);
int pageDescAge = 990;
@ -121,6 +121,7 @@ namespace ShardingCore.Test50_3x
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"));
@ -241,6 +242,7 @@ namespace ShardingCore.Test50_3x
var sysUserModDesc = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Id).FirstOrDefaultAsync();
Assert.True(sysUserModDesc != null && sysUserModDesc.Id == "999");
}
[Fact]
@ -248,6 +250,8 @@ namespace ShardingCore.Test50_3x
{
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefaultAsync();
Assert.NotNull(sysUserMod);
var userMod = _virtualDbContext.Set<SysUserMod>().Find("1");
Assert.Equal(sysUserMod, userMod);
Assert.True(sysUserMod.Id == "1");
var user198 = await _virtualDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.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();
Assert.NotNull(sysUserMod);
Assert.Equal("2", sysUserMod.Id);
}
[Fact]

View File

@ -47,6 +47,7 @@ namespace ShardingCore.Test50_3x
{
o.CreateShardingTableOnStart = true;
o.EnsureCreatedWithOutShardingTable = true;
o.AutoTrackEntity = true;
})
.AddShardingTransaction((connection, builder) =>
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))