55using System . Text ;
66using Microsoft . EntityFrameworkCore ;
77using Microsoft . EntityFrameworkCore . Metadata ;
8+ using Microsoft . EntityFrameworkCore . Metadata . Internal ;
9+ using Microsoft . Extensions . Logging ;
10+ using ShardingCore . Core . ShardingConfigurations ;
811using ShardingCore . Exceptions ;
912using ShardingCore . Extensions ;
1013using ShardingCore . Sharding . Abstractions ;
@@ -16,9 +19,14 @@ namespace ShardingCore.Core.EntityMetadatas
1619 /// </summary>
1720 public class DefaultEntityMetadataManager : IEntityMetadataManager
1821 {
22+ private readonly ShardingConfigOptions _shardingConfigOptions ;
1923 private readonly ConcurrentDictionary < Type , EntityMetadata > _caches = new ( ) ;
2024 private readonly ConcurrentDictionary < string /*logic table name*/ , List < EntityMetadata > > _logicTableCaches = new ( ) ;
2125
26+ public DefaultEntityMetadataManager ( ShardingConfigOptions shardingConfigOptions )
27+ {
28+ _shardingConfigOptions = shardingConfigOptions ;
29+ }
2230 public bool AddEntityMetadata ( EntityMetadata entityMetadata )
2331 {
2432 return _caches . TryAdd ( entityMetadata . EntityType , entityMetadata ) ;
@@ -104,6 +112,33 @@ public bool TryInitModel(IEntityType efEntityType)
104112 {
105113 if ( _caches . TryGetValue ( efEntityType . ClrType , out var metadata ) )
106114 {
115+ if ( _shardingConfigOptions . CheckShardingKeyValueGenerated )
116+ {
117+ if ( metadata . IsMultiDataSourceMapping )
118+ {
119+ foreach ( var metadataProperty in metadata . ShardingDataSourceProperties )
120+ {
121+ var propertyName = metadataProperty . Key ;
122+ var property = efEntityType . GetProperty ( propertyName ) ;
123+ if ( property . ValueGenerated != ValueGenerated . Never )
124+ {
125+ throw new ShardingCoreConfigException ( $ "sharding data source key:{ propertyName } is not { nameof ( ValueGenerated ) } .{ nameof ( ValueGenerated . Never ) } ") ;
126+ }
127+ }
128+ }
129+ if ( metadata . IsMultiTableMapping )
130+ {
131+ foreach ( var metadataProperty in metadata . ShardingTableProperties )
132+ {
133+ var propertyName = metadataProperty . Key ;
134+ var property = efEntityType . GetProperty ( propertyName ) ;
135+ if ( property . ValueGenerated != ValueGenerated . Never )
136+ {
137+ throw new ShardingCoreConfigException ( $ "sharding table key:{ propertyName } is not { nameof ( ValueGenerated ) } .{ nameof ( ValueGenerated . Never ) } ") ;
138+ }
139+ }
140+ }
141+ }
107142 metadata . SetEntityModel ( efEntityType ) ;
108143 if ( string . IsNullOrWhiteSpace ( metadata . LogicTableName ) )
109144 {
0 commit comments