From 41571e89b70bb5859ac41482498f112ad31283ea Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Sun, 9 Oct 2022 16:39:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=92=88=E5=AF=B9=E5=88=86?= =?UTF-8?q?=E7=89=87=E9=94=AE=E7=9A=84ValueGenerated=E7=9A=84=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E5=88=A4=E6=96=AD=E5=8F=AA=E5=85=81=E8=AE=B8Never?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DefaultEntityMetadataManager.cs | 35 +++++++++++++++++++ .../ShardingConfigOptions.cs | 6 ++++ 2 files changed, 41 insertions(+) diff --git a/src/ShardingCore/Core/EntityMetadatas/DefaultEntityMetadataManager.cs b/src/ShardingCore/Core/EntityMetadatas/DefaultEntityMetadataManager.cs index a3274b0b..31c34b82 100644 --- a/src/ShardingCore/Core/EntityMetadatas/DefaultEntityMetadataManager.cs +++ b/src/ShardingCore/Core/EntityMetadatas/DefaultEntityMetadataManager.cs @@ -5,6 +5,9 @@ using System.Linq; using System.Text; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using Microsoft.Extensions.Logging; +using ShardingCore.Core.ShardingConfigurations; using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Sharding.Abstractions; @@ -16,9 +19,14 @@ namespace ShardingCore.Core.EntityMetadatas /// public class DefaultEntityMetadataManager : IEntityMetadataManager { + private readonly ShardingConfigOptions _shardingConfigOptions; private readonly ConcurrentDictionary _caches = new(); private readonly ConcurrentDictionary> _logicTableCaches = new(); + public DefaultEntityMetadataManager(ShardingConfigOptions shardingConfigOptions) + { + _shardingConfigOptions = shardingConfigOptions; + } public bool AddEntityMetadata(EntityMetadata entityMetadata) { return _caches.TryAdd(entityMetadata.EntityType, entityMetadata); @@ -104,6 +112,33 @@ namespace ShardingCore.Core.EntityMetadatas { if (_caches.TryGetValue(efEntityType.ClrType, out var metadata)) { + if (_shardingConfigOptions.CheckShardingKeyValueGenerated) + { + if (metadata.IsMultiDataSourceMapping) + { + foreach (var metadataProperty in metadata.ShardingDataSourceProperties) + { + var propertyName = metadataProperty.Key; + var property = efEntityType.GetProperty(propertyName); + if (property.ValueGenerated!=ValueGenerated.Never) + { + throw new ShardingCoreConfigException($"sharding data source key:{propertyName} is not {nameof(ValueGenerated)}.{nameof(ValueGenerated.Never)}"); + } + } + } + if (metadata.IsMultiTableMapping) + { + foreach (var metadataProperty in metadata.ShardingTableProperties) + { + var propertyName = metadataProperty.Key; + var property = efEntityType.GetProperty(propertyName); + if (property.ValueGenerated!=ValueGenerated.Never) + { + throw new ShardingCoreConfigException($"sharding table key:{propertyName} is not {nameof(ValueGenerated)}.{nameof(ValueGenerated.Never)}"); + } + } + } + } metadata.SetEntityModel(efEntityType); if (string.IsNullOrWhiteSpace(metadata.LogicTableName)) { diff --git a/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs b/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs index ced9db20..4d5d2a2b 100644 --- a/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs +++ b/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs @@ -55,6 +55,12 @@ namespace ShardingCore.Core.ShardingConfigurations /// 默认数据源链接字符串 /// public string DefaultConnectionString { get; private set; } + + /// + /// 检测分片键的自动值是否有疑义 + /// + + public bool CheckShardingKeyValueGenerated { get; set; } = true; /// /// 添加默认数据源 ///