diff --git a/README.md b/README.md index 105b8515..d2d0c73b 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ---
-

Github Star 助力dotnet 生态 Gitee Star

+

Gitee Star 助力dotnet 生态 Github Star

--- diff --git a/samples/Sample.Migrations/readme.md b/samples/Sample.Migrations/readme.md index b9850a6b..6379421d 100644 --- a/samples/Sample.Migrations/readme.md +++ b/samples/Sample.Migrations/readme.md @@ -1,4 +1,4 @@ -本项目迁移参考[efcore.sharding](https://github.com/Coldairarrow/EFCore.Sharding/tree/master/examples/Demo.DbMigrator) +本项目迁移参考[efcore.sharding](https://github.com/Coldairarrow/EFCore.Sharding/tree/master/examples/Demo.DbMigrator) 后续会不断完善 ## 迁移步骤 1. 执行迁移命令,但不执行更新命令 diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IRouteTail.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IRouteTail.cs index 1fc2db64..c221fa05 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IRouteTail.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IRouteTail.cs @@ -10,5 +10,6 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions { string GetRouteTailIdentity(); bool IsMultiEntityQuery(); + bool IsShardingTableQuery(); } } \ No newline at end of file diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/MultiQueryRouteTail.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/MultiQueryRouteTail.cs index ae711836..a647500c 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/MultiQueryRouteTail.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/MultiQueryRouteTail.cs @@ -19,6 +19,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails private readonly TableRouteResult _tableRouteResult; private readonly string _modelCacheKey; private readonly ISet _entityTypes; + private readonly bool _isShardingTableQuery; public MultiQueryRouteTail(TableRouteResult tableRouteResult) { @@ -26,6 +27,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails _tableRouteResult = tableRouteResult; _modelCacheKey = RANDOM_MODEL_CACHE_KEY+Guid.NewGuid().ToString("n"); _entityTypes = tableRouteResult.ReplaceTables.Select(o=>o.EntityType).ToHashSet(); + _isShardingTableQuery = _entityTypes.Any(o => o.IsShardingTable()); } public string GetRouteTailIdentity() { @@ -37,6 +39,11 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails return true; } + public bool IsShardingTableQuery() + { + return _isShardingTableQuery; + } + public string GetEntityTail(Type entityType) { return _tableRouteResult.ReplaceTables.Single(o => o.EntityType == entityType).Tail; diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/SingleQueryRouteTail.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/SingleQueryRouteTail.cs index 79ef6cb9..8bb071d9 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/SingleQueryRouteTail.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/SingleQueryRouteTail.cs @@ -17,6 +17,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails private readonly TableRouteResult _tableRouteResult; private readonly string _tail; private readonly string _modelCacheKey; + private readonly bool _isShardingTableQuery; public SingleQueryRouteTail(TableRouteResult tableRouteResult) { @@ -24,12 +25,14 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails _tableRouteResult = tableRouteResult; _tail= _tableRouteResult.ReplaceTables.First().Tail; _modelCacheKey = _tail.FormatRouteTail2ModelCacheKey(); + _isShardingTableQuery = !string.IsNullOrWhiteSpace(_tail); } public SingleQueryRouteTail(string tail) { _tail= tail; _modelCacheKey = _tail.FormatRouteTail2ModelCacheKey(); + _isShardingTableQuery = !string.IsNullOrWhiteSpace(_tail); } public virtual string GetRouteTailIdentity() { @@ -41,6 +44,11 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails return false; } + public bool IsShardingTableQuery() + { + return _isShardingTableQuery; + } + public virtual string GetTail() { return _tail; diff --git a/src/ShardingCore/EFCores/ShardingModelCustomizer.cs b/src/ShardingCore/EFCores/ShardingModelCustomizer.cs index e423c342..4875a42d 100644 --- a/src/ShardingCore/EFCores/ShardingModelCustomizer.cs +++ b/src/ShardingCore/EFCores/ShardingModelCustomizer.cs @@ -33,7 +33,7 @@ namespace ShardingCore.EFCores public override void Customize(ModelBuilder modelBuilder, DbContext context) { base.Customize(modelBuilder, context); - if (context is IShardingTableDbContext shardingTableDbContext) + if (context is IShardingTableDbContext shardingTableDbContext&& shardingTableDbContext.RouteTail.IsShardingTableQuery()) { var isMultiEntityQuery = shardingTableDbContext.RouteTail.IsMultiEntityQuery(); if (!isMultiEntityQuery) @@ -44,7 +44,7 @@ namespace ShardingCore.EFCores var typeMap = virtualTableManager.GetAllVirtualTables().Where(o => o.GetTableAllTails().Contains(tail)).Select(o => o.EntityType).ToHashSet(); //设置分表 - var mutableEntityTypes = modelBuilder.Model.GetEntityTypes().Where(o => o.ClrType.IsShardingTable() && typeMap.Contains(o.ClrType)); + var mutableEntityTypes = modelBuilder.Model.GetEntityTypes().Where(o => o.ClrType.IsShardingTable() && typeMap.Contains(o.ClrType)).ToArray(); foreach (var entityType in mutableEntityTypes) { MappingToTable(entityType.ClrType, modelBuilder, tail);