优化查询时模型创建判断

This commit is contained in:
xuejiaming 2021-10-07 15:40:39 +08:00
parent 0d26648fc3
commit 2a13b37ba4
6 changed files with 20 additions and 4 deletions

View File

@ -6,7 +6,7 @@
--- ---
<div align="center"> <div align="center">
<p> <a href="https://github.com/xuejmnet/sharding-core">Github Star</a> 助力dotnet 生态 <a href="https://gitee.com/dotnetchina/sharding-core">Gitee Star</a> </p> <p> <a href="https://gitee.com/dotnetchina/sharding-core">Gitee Star</a> 助力dotnet 生态 <a href="https://github.com/xuejmnet/sharding-core">Github Star</a> </p>
</div> </div>
--- ---

View File

@ -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. 执行迁移命令,但不执行更新命令 1. 执行迁移命令,但不执行更新命令

View File

@ -10,5 +10,6 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions
{ {
string GetRouteTailIdentity(); string GetRouteTailIdentity();
bool IsMultiEntityQuery(); bool IsMultiEntityQuery();
bool IsShardingTableQuery();
} }
} }

View File

@ -19,6 +19,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails
private readonly TableRouteResult _tableRouteResult; private readonly TableRouteResult _tableRouteResult;
private readonly string _modelCacheKey; private readonly string _modelCacheKey;
private readonly ISet<Type> _entityTypes; private readonly ISet<Type> _entityTypes;
private readonly bool _isShardingTableQuery;
public MultiQueryRouteTail(TableRouteResult tableRouteResult) public MultiQueryRouteTail(TableRouteResult tableRouteResult)
{ {
@ -26,6 +27,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails
_tableRouteResult = tableRouteResult; _tableRouteResult = tableRouteResult;
_modelCacheKey = RANDOM_MODEL_CACHE_KEY+Guid.NewGuid().ToString("n"); _modelCacheKey = RANDOM_MODEL_CACHE_KEY+Guid.NewGuid().ToString("n");
_entityTypes = tableRouteResult.ReplaceTables.Select(o=>o.EntityType).ToHashSet(); _entityTypes = tableRouteResult.ReplaceTables.Select(o=>o.EntityType).ToHashSet();
_isShardingTableQuery = _entityTypes.Any(o => o.IsShardingTable());
} }
public string GetRouteTailIdentity() public string GetRouteTailIdentity()
{ {
@ -37,6 +39,11 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails
return true; return true;
} }
public bool IsShardingTableQuery()
{
return _isShardingTableQuery;
}
public string GetEntityTail(Type entityType) public string GetEntityTail(Type entityType)
{ {
return _tableRouteResult.ReplaceTables.Single(o => o.EntityType == entityType).Tail; return _tableRouteResult.ReplaceTables.Single(o => o.EntityType == entityType).Tail;

View File

@ -17,6 +17,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails
private readonly TableRouteResult _tableRouteResult; private readonly TableRouteResult _tableRouteResult;
private readonly string _tail; private readonly string _tail;
private readonly string _modelCacheKey; private readonly string _modelCacheKey;
private readonly bool _isShardingTableQuery;
public SingleQueryRouteTail(TableRouteResult tableRouteResult) public SingleQueryRouteTail(TableRouteResult tableRouteResult)
{ {
@ -24,12 +25,14 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails
_tableRouteResult = tableRouteResult; _tableRouteResult = tableRouteResult;
_tail= _tableRouteResult.ReplaceTables.First().Tail; _tail= _tableRouteResult.ReplaceTables.First().Tail;
_modelCacheKey = _tail.FormatRouteTail2ModelCacheKey(); _modelCacheKey = _tail.FormatRouteTail2ModelCacheKey();
_isShardingTableQuery = !string.IsNullOrWhiteSpace(_tail);
} }
public SingleQueryRouteTail(string tail) public SingleQueryRouteTail(string tail)
{ {
_tail= tail; _tail= tail;
_modelCacheKey = _tail.FormatRouteTail2ModelCacheKey(); _modelCacheKey = _tail.FormatRouteTail2ModelCacheKey();
_isShardingTableQuery = !string.IsNullOrWhiteSpace(_tail);
} }
public virtual string GetRouteTailIdentity() public virtual string GetRouteTailIdentity()
{ {
@ -41,6 +44,11 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails
return false; return false;
} }
public bool IsShardingTableQuery()
{
return _isShardingTableQuery;
}
public virtual string GetTail() public virtual string GetTail()
{ {
return _tail; return _tail;

View File

@ -33,7 +33,7 @@ namespace ShardingCore.EFCores
public override void Customize(ModelBuilder modelBuilder, DbContext context) public override void Customize(ModelBuilder modelBuilder, DbContext context)
{ {
base.Customize(modelBuilder, context); base.Customize(modelBuilder, context);
if (context is IShardingTableDbContext shardingTableDbContext) if (context is IShardingTableDbContext shardingTableDbContext&& shardingTableDbContext.RouteTail.IsShardingTableQuery())
{ {
var isMultiEntityQuery = shardingTableDbContext.RouteTail.IsMultiEntityQuery(); var isMultiEntityQuery = shardingTableDbContext.RouteTail.IsMultiEntityQuery();
if (!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 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) foreach (var entityType in mutableEntityTypes)
{ {
MappingToTable(entityType.ClrType, modelBuilder, tail); MappingToTable(entityType.ClrType, modelBuilder, tail);