From 922f64524922447eb548d0fcfb734c7d77596a10 Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Tue, 26 Sep 2023 22:54:15 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=B8=BA=E5=A4=9A=E5=AE=9E=E4=BE=8B=20?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E8=A1=A8=E5=90=8D=E8=A7=84=E5=88=99=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8B=E5=BC=80=E6=94=BE=E7=BC=93=E5=AD=98=E6=9C=BA?= =?UTF-8?q?=E5=88=B6=EF=BC=8C=E5=8F=AF=E4=BD=BF=E7=94=A8MemoryCache?= =?UTF-8?q?=E7=AD=89=E5=8A=9F=E8=83=BD=E6=9B=BF=E6=8D=A2=E4=B8=BA=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 避免全局静态缓存表名称无法替换 --- FreeSql/Internal/UtilsExpressionTree.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index aa95c1cf..683406f6 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -19,8 +19,10 @@ namespace FreeSql.Internal { public class Utils { - - static ConcurrentDictionary> _cacheGetTableByEntity = new ConcurrentDictionary>(); + public static ConcurrentDictionary> _cacheGetTableByEntity + { + get; set; + } = new ConcurrentDictionary>(); internal static void RemoveTableByEntity(Type entity, CommonUtils common) { if (entity.IsAnonymousType() || @@ -534,7 +536,7 @@ namespace FreeSql.Internal { col.Attribute.IsNullable = false; col.Attribute.DbType = col.Attribute.DbType.Replace("NOT NULL", "").Replace(" NULL", "").Trim(); - switch(common._orm.Ado.DataType) + switch (common._orm.Ado.DataType) { case DataType.Sqlite: col.Attribute.DbType += " NOT NULL"; //sqlite 主键也可以插入 null From 3c7d67fcc2d85b0facdf80ef35d406838f1b1ae8 Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Wed, 27 Sep 2023 00:54:43 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A4=9A=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=E6=83=85=E5=86=B5=E4=B8=8B=E7=9A=84=E9=9D=99=E6=80=81?= =?UTF-8?q?=E9=9B=86=E5=90=88=E7=BC=93=E5=AD=98=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql/Internal/UtilsExpressionTree.cs | 38 +++++++++++++++++-------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index 683406f6..352ca6d4 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -19,10 +19,24 @@ namespace FreeSql.Internal { public class Utils { + /// + /// 用于解决多实例情况下的静态集合缓存问题 + /// + public static Func>> ChacheTableEntityFactory = null; + private static ConcurrentDictionary> __cacheGetTableByEntity; public static ConcurrentDictionary> _cacheGetTableByEntity { - get; set; - } = new ConcurrentDictionary>(); + get + { + + if (ChacheTableEntityFactory != null) + { + return ChacheTableEntityFactory.Invoke(); + } + __cacheGetTableByEntity ??= new ConcurrentDictionary>(); + return __cacheGetTableByEntity; + } + } internal static void RemoveTableByEntity(Type entity, CommonUtils common) { if (entity.IsAnonymousType() || @@ -1782,16 +1796,16 @@ namespace FreeSql.Internal Expression.IfThenElse(Expression.Equal(read2ExpValue, Expression.Constant(null)), Expression.Assign(Expression.MakeMemberAccess(ret2Exp, field), Expression.Default(field.FieldType)), Expression.Assign(Expression.MakeMemberAccess(ret2Exp, field), Expression.Convert(read2ExpValue, field.FieldType))) - //), - //Expression.Catch(typeof(Exception), Expression.Block( - // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(0)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 0)))), - // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(1)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 1)))), - // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(2)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 2)))), - // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(3)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 3)))), - // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(4)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 4)))) - // ) - //)) - }); + //), + //Expression.Catch(typeof(Exception), Expression.Block( + // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(0)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 0)))), + // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(1)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 1)))), + // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(2)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 2)))), + // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(3)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 3)))), + // Expression.IfThen(Expression.Equal(read2ExpDataIndex, Expression.Constant(4)), Expression.Throw(Expression.Constant(new Exception(field.Name + "," + 4)))) + // ) + //)) + }); } block2Exp.AddRange(new Expression[] { Expression.Return(returnTarget, Expression.New(RowInfo.Constructor, Expression.Convert(ret2Exp, typeof(object)), dataIndexExp)), From 456c09152c512593a3bc865fe60962928cb0b1a6 Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Wed, 27 Sep 2023 00:56:37 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql/Internal/UtilsExpressionTree.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index 352ca6d4..ebd6d9ea 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -28,7 +28,6 @@ namespace FreeSql.Internal { get { - if (ChacheTableEntityFactory != null) { return ChacheTableEntityFactory.Invoke(); From be5b2cc044457836ddd7c82a2f6c8310aded910a Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Wed, 27 Sep 2023 01:03:14 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=90=91FreeSqlBuilder=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20=E8=A1=A8=E4=BF=A1=E6=81=AF=E6=98=A0=E5=B0=84?= =?UTF-8?q?=E5=B7=A5=E5=8E=82=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql/FreeSqlBuilder.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/FreeSql/FreeSqlBuilder.cs b/FreeSql/FreeSqlBuilder.cs index 3c3719ac..37fad903 100644 --- a/FreeSql/FreeSqlBuilder.cs +++ b/FreeSql/FreeSqlBuilder.cs @@ -11,6 +11,7 @@ using System.Linq.Expressions; using System.Runtime; using FreeSql.Internal.Model.Interface; using System.Threading; +using FreeSql.Internal.Model; namespace FreeSql { @@ -50,6 +51,18 @@ namespace FreeSql _providerType = providerType; return this; } + + /// + /// 用于指定自定义实现TableEntiy 的缓存集合 + /// 解决多实例下相同类型映射到不同表的问题 + /// + /// + /// + public FreeSqlBuilder UseCustomTableEntityCacheFactory(Func>> factory) + { + Utils.ChacheTableEntityFactory = factory; + return this; + } /// /// 使用原始连接池(ado.net、odbc、oledb) /// 默认:false @@ -63,7 +76,7 @@ namespace FreeSql /// public FreeSqlBuilder UseAdoConnectionPool(bool value) { - _isAdoConnectionPool = value ; + _isAdoConnectionPool = value; return this; } /// @@ -577,7 +590,8 @@ namespace FreeSql { FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add((LabelTarget returnTarget, Expression valueExp, Type type2) => { - if (FreeSql.Internal.Utils.TypeHandlers.TryGetValue(type2, out var typeHandler)) { + if (FreeSql.Internal.Utils.TypeHandlers.TryGetValue(type2, out var typeHandler)) + { var valueExpRet = Expression.Call( Expression.Constant(typeHandler, typeof(ITypeHandler)), typeof(ITypeHandler).GetMethod(nameof(typeHandler.Deserialize)), From a278578daf50b8cbe59be87224e8584a840cab8e Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Wed, 27 Sep 2023 01:22:37 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BD=8E=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=85=BC=E5=AE=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql/FreeSql.xml | 13 +++++++++++++ FreeSql/Internal/UtilsExpressionTree.cs | 3 +-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 9b5affeb..88245752 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -1445,6 +1445,14 @@ 提供者的类型,一般不需要指定,如果一直提示“缺少 FreeSql 数据库实现包:FreeSql.Provider.MySql.dll,可前往 nuget 下载”的错误,说明反射获取不到类型,此时该参数可排上用场例如:typeof(FreeSql.SqlServer.SqlServerProvider<>) + + + 用于指定自定义实现TableEntiy 的缓存集合 + 解决多实例下相同类型映射到不同表的问题 + + + + 使用原始连接池(ado.net、odbc、oledb) @@ -5002,6 +5010,11 @@ + + + 用于解决多实例情况下的静态集合缓存问题 + + diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index ebd6d9ea..03155a2e 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -23,7 +23,7 @@ namespace FreeSql.Internal /// 用于解决多实例情况下的静态集合缓存问题 /// public static Func>> ChacheTableEntityFactory = null; - private static ConcurrentDictionary> __cacheGetTableByEntity; + private static ConcurrentDictionary> __cacheGetTableByEntity = new ConcurrentDictionary>(); public static ConcurrentDictionary> _cacheGetTableByEntity { get @@ -32,7 +32,6 @@ namespace FreeSql.Internal { return ChacheTableEntityFactory.Invoke(); } - __cacheGetTableByEntity ??= new ConcurrentDictionary>(); return __cacheGetTableByEntity; } }