修复非sharding对象不能使用find,发布x.6.0.9

This commit is contained in:
xuejiaming 2022-07-12 15:27:09 +08:00
parent 636a643d2c
commit 6cf015a0e6
4 changed files with 35 additions and 17 deletions

View File

@ -1,9 +1,9 @@
:start
::定义版本
set EFCORE2=2.6.0.8
set EFCORE3=3.6.0.8
set EFCORE5=5.6.0.8
set EFCORE6=6.6.0.8
set EFCORE2=2.6.0.9
set EFCORE3=3.6.0.9
set EFCORE5=5.6.0.9
set EFCORE6=6.6.0.9
::删除所有bin与obj下的文件
@echo off

View File

@ -155,16 +155,14 @@ namespace Sample.MySql
using (var scope = app.ApplicationServices.CreateScope())
{
var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
// if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
{
defaultShardingDbContext.Database.Migrate();
}
}
Stopwatch sp = Stopwatch.StartNew();
app.ApplicationServices.UseAutoTryCompensateTable();
sp.Stop();
Console.WriteLine("UseAutoTryCompensateTable:"+sp.ElapsedMilliseconds);
app.ApplicationServices.UseAutoTryCompensateTable(12);
app.UseRouting();
app.UseAuthorization();

View File

@ -20,10 +20,12 @@ namespace ShardingCore.Core.ShardingConfigurations
public bool? IgnoreCreateTableError { get; set; } = false;
/// <summary>
/// 配置全局迁移最大并行数,以data source为一个单元并行迁移保证在多数据库分库情况下可以大大提高性能
/// 默认系统逻辑处理器<code>Environment.ProcessorCount</code>
/// </summary>
public int MigrationParallelCount { get; set; }= Environment.ProcessorCount;
/// <summary>
/// 启动补偿表的最大并行数,以data source为一个单元并行迁移保证在多数据库分库情况下可以大大提高性能
/// 默认系统逻辑处理器<code>Environment.ProcessorCount</code>
/// </summary>
public int CompensateTableParallelCount { get; set; }= Environment.ProcessorCount;
/// <summary>

View File

@ -12,6 +12,7 @@ using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Utils;
@ -394,34 +395,51 @@ namespace ShardingCore.EFCores
private DbContext GetDbContextByKeyValue(params object[] keyValues)
{
var entityMetadata = EntityMetadataManager.TryGet(typeof(TEntity));
if (entityMetadata == null)
{
throw new ShardingCoreInvalidOperationException(
$"cant found type:[{typeof(TEntity)}] in {nameof(IEntityMetadataManager)}");
}
//既不是分表也不是分库的话就是默认对象
if (!entityMetadata.IsShardingTable() && !entityMetadata.IsShardingDataSource())
{
var defaultDataSourceName = _shardingRuntimeContext.GetVirtualDataSource().DefaultDataSourceName;
var routeTailFactory = _shardingRuntimeContext.GetRouteTailFactory();
var routeTail = routeTailFactory.Create(string.Empty);
return _context.GetShareDbContext(defaultDataSourceName, routeTail);
}
if (keyValues.Length == 1)
{
var entityMetadata = EntityMetadataManager.TryGet(typeof(TEntity));
//单key字段
if (null != entityMetadata && entityMetadata.IsSingleKey)
if (entityMetadata.IsSingleKey)
{
var isShardingDataSource = entityMetadata.IsShardingDataSource();
var shardingDataSourceFieldIsKey = entityMetadata.ShardingDataSourceFieldIsKey();
if (isShardingDataSource && !shardingDataSourceFieldIsKey)
return null;
{
throw new ShardingCoreNotSupportException("multi data source entity find key should sharding value");
}
var isShardingTable = entityMetadata.IsShardingTable();
var shardingTableFieldIsKey = entityMetadata.ShardingTableFieldIsKey();
if (isShardingTable && !shardingTableFieldIsKey)
return null;
{
throw new ShardingCoreNotSupportException("multi table entity find key should sharding value");
}
var primaryKeyValue = keyValues[0];
if (primaryKeyValue != null)
{
var dataSourceName = GetDataSourceName(primaryKeyValue);
var tableTail = TableRouteManager.GetTableTail<TEntity>(dataSourceName, primaryKeyValue);
var routeTail = _shardingRuntimeContext.GetRouteTailFactory().Create(tableTail);
;
return _context.GetShareDbContext(dataSourceName, routeTail);
}
}
}
return null;
throw new ShardingCoreNotSupportException("sharding entity multi key");
}
private string GetDataSourceName(object shardingKeyValue)