|
12 | 12 | using ShardingCore.Core.RuntimeContexts; |
13 | 13 | using ShardingCore.Core.VirtualDatabase.VirtualDataSources; |
14 | 14 | using ShardingCore.Core.VirtualRoutes.Abstractions; |
| 15 | +using ShardingCore.Exceptions; |
15 | 16 | using ShardingCore.Extensions; |
16 | 17 | using ShardingCore.Utils; |
17 | 18 |
|
@@ -394,34 +395,51 @@ public override Task<TEntity> FindAsync(object[] keyValues, CancellationToken ca |
394 | 395 |
|
395 | 396 | private DbContext GetDbContextByKeyValue(params object[] keyValues) |
396 | 397 | { |
397 | | - if (keyValues.Length == 1) |
| 398 | + var entityMetadata = EntityMetadataManager.TryGet(typeof(TEntity)); |
| 399 | + if (entityMetadata == null) |
| 400 | + { |
| 401 | + throw new ShardingCoreInvalidOperationException( |
| 402 | + $"cant found type:[{typeof(TEntity)}] in {nameof(IEntityMetadataManager)}"); |
| 403 | + } |
| 404 | + |
| 405 | + //既不是分表也不是分库的话就是默认对象 |
| 406 | + if (!entityMetadata.IsShardingTable() && !entityMetadata.IsShardingDataSource()) |
398 | 407 | { |
399 | | - var entityMetadata = EntityMetadataManager.TryGet(typeof(TEntity)); |
| 408 | + var defaultDataSourceName = _shardingRuntimeContext.GetVirtualDataSource().DefaultDataSourceName; |
| 409 | + var routeTailFactory = _shardingRuntimeContext.GetRouteTailFactory(); |
| 410 | + var routeTail = routeTailFactory.Create(string.Empty); |
| 411 | + return _context.GetShareDbContext(defaultDataSourceName, routeTail); |
| 412 | + } |
400 | 413 |
|
| 414 | + if (keyValues.Length == 1) |
| 415 | + { |
401 | 416 | //单key字段 |
402 | | - if (null != entityMetadata && entityMetadata.IsSingleKey) |
| 417 | + if (entityMetadata.IsSingleKey) |
403 | 418 | { |
404 | 419 | var isShardingDataSource = entityMetadata.IsShardingDataSource(); |
405 | 420 | var shardingDataSourceFieldIsKey = entityMetadata.ShardingDataSourceFieldIsKey(); |
406 | 421 | if (isShardingDataSource && !shardingDataSourceFieldIsKey) |
407 | | - return null; |
| 422 | + { |
| 423 | + throw new ShardingCoreNotSupportException("multi data source entity find key should sharding value"); |
| 424 | + } |
408 | 425 | var isShardingTable = entityMetadata.IsShardingTable(); |
409 | 426 | var shardingTableFieldIsKey = entityMetadata.ShardingTableFieldIsKey(); |
410 | 427 | if (isShardingTable && !shardingTableFieldIsKey) |
411 | | - return null; |
| 428 | + { |
| 429 | + throw new ShardingCoreNotSupportException("multi table entity find key should sharding value"); |
| 430 | + } |
412 | 431 | var primaryKeyValue = keyValues[0]; |
413 | 432 | if (primaryKeyValue != null) |
414 | 433 | { |
415 | 434 | var dataSourceName = GetDataSourceName(primaryKeyValue); |
416 | | - var tableTail = TableRouteManager.GetTableTail<TEntity>(dataSourceName,primaryKeyValue); |
| 435 | + var tableTail = TableRouteManager.GetTableTail<TEntity>(dataSourceName, primaryKeyValue); |
417 | 436 | var routeTail = _shardingRuntimeContext.GetRouteTailFactory().Create(tableTail); |
418 | | - ; |
419 | 437 | return _context.GetShareDbContext(dataSourceName, routeTail); |
420 | 438 | } |
421 | 439 | } |
422 | 440 | } |
423 | 441 |
|
424 | | - return null; |
| 442 | + throw new ShardingCoreNotSupportException("sharding entity multi key"); |
425 | 443 | } |
426 | 444 |
|
427 | 445 | private string GetDataSourceName(object shardingKeyValue) |
|
0 commit comments