diff --git a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs index 13c44e92..da2475d1 100644 --- a/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs +++ b/Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs @@ -8,7 +8,9 @@ using System.Linq; using System.Linq.Expressions; using System.Threading; -// ReSharper disable once CheckNamespace +// ReSharper disable CheckNamespace +// ReSharper disable InconsistentNaming +// ReSharper disable InconsistentlySynchronizedField namespace FreeSql { /// @@ -19,13 +21,16 @@ namespace FreeSql { internal static IFreeSql _ormPriv; + private const string ErrorMessageTemplate = @"使用前请初始化: +BaseEntity.Initialization(new FreeSqlBuilder() + .UseAutoSyncStructure(true) + .UseConnectionString(DataType.Sqlite, ""data source=test.db;max pool size=5"") + .Build());"; + /// /// 全局 IFreeSql orm 对象 /// - public static IFreeSql Orm => _ormPriv ?? throw new Exception(@"使用前请初始化 BaseEntity.Initialization(new FreeSqlBuilder() -.UseAutoSyncStructure(true) -.UseConnectionString(DataType.Sqlite, ""data source=test.db;max pool size=5"") -.Build());"); + public static IFreeSql Orm => _ormPriv ?? throw new Exception(ErrorMessageTemplate); internal static Func _resolveUow; @@ -63,8 +68,8 @@ namespace FreeSql public Action Fluent; } - static ConcurrentQueue _configEntityQueues = new(); - static object _configEntityLock = new(); + static readonly ConcurrentQueue _configEntityQueues = new(); + static readonly object _configEntityLock = new(); internal static void ConfigEntity(Type entityType, Action fluent) { @@ -131,26 +136,29 @@ namespace FreeSql return; var isFirst = true; - - IBaseRepository berepo = null; + IBaseRepository baseRepo = null; foreach (var item in ie) { - if (item is null) return; + if (item is null) + { + return; + } + if (isFirst) { isFirst = false; var itemType = item.GetType(); if (itemType == typeof(object)) return; - if (itemType.FullName.Contains("FreeSqlLazyEntity__")) itemType = itemType.BaseType; + if (itemType.FullName!.Contains("FreeSqlLazyEntity__")) itemType = itemType.BaseType; if (Orm.CodeFirst.GetTableByEntity(itemType)?.Primarys.Any() != true) return; - if (item is BaseEntity == false) return; + if (item is not BaseEntity) return; } if (item is BaseEntity entity) { - berepo ??= Orm.GetRepository(); - entity.Repository = berepo; + baseRepo ??= Orm.GetRepository(); + entity.Repository = baseRepo; entity.Attach(); } }