5
5
using System . Text ;
6
6
using Microsoft . EntityFrameworkCore ;
7
7
using Microsoft . EntityFrameworkCore . Metadata ;
8
+ using ShardingCore . Exceptions ;
8
9
using ShardingCore . Extensions ;
9
10
using ShardingCore . Sharding . Abstractions ;
10
11
@@ -16,6 +17,7 @@ namespace ShardingCore.Core.EntityMetadatas
16
17
public class DefaultEntityMetadataManager : IEntityMetadataManager
17
18
{
18
19
private readonly ConcurrentDictionary < Type , EntityMetadata > _caches = new ( ) ;
20
+ private readonly ConcurrentDictionary < string /*logic table name*/ , EntityMetadata > _logicTableCaches = new ( ) ;
19
21
20
22
public bool AddEntityMetadata ( EntityMetadata entityMetadata )
21
23
{
@@ -70,7 +72,11 @@ public EntityMetadata TryGet(Type entityType)
70
72
71
73
public EntityMetadata TryGetByLogicTableName ( string logicTableName )
72
74
{
73
- return _caches . Values . FirstOrDefault ( o => o . LogicTableName == logicTableName ) ;
75
+ if ( _logicTableCaches . TryGetValue ( logicTableName , out var metadata ) )
76
+ {
77
+ return metadata ;
78
+ }
79
+ return null ;
74
80
}
75
81
76
82
/// <summary>
@@ -99,6 +105,16 @@ public bool TryInitModel(IEntityType efEntityType)
99
105
if ( _caches . TryGetValue ( efEntityType . ClrType , out var metadata ) )
100
106
{
101
107
metadata . SetEntityModel ( efEntityType ) ;
108
+ if ( string . IsNullOrWhiteSpace ( metadata . LogicTableName ) )
109
+ {
110
+ throw new ShardingCoreInvalidOperationException (
111
+ $ "init model error, cant get logic table name:[{ metadata . LogicTableName } ] from entity:[{ efEntityType . ClrType } ]") ;
112
+ }
113
+ if ( ! _logicTableCaches . TryAdd ( metadata . LogicTableName , metadata ) )
114
+ {
115
+ throw new ShardingCoreInvalidOperationException (
116
+ $ "cant add logic table name caches for metadata:[{ metadata . LogicTableName } -{ efEntityType . ClrType } ]") ;
117
+ }
102
118
return true ;
103
119
}
104
120
0 commit comments