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/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)), diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index aa95c1cf..03155a2e 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -19,8 +19,22 @@ namespace FreeSql.Internal { public class Utils { - - static ConcurrentDictionary> _cacheGetTableByEntity = new ConcurrentDictionary>(); + /// + /// 用于解决多实例情况下的静态集合缓存问题 + /// + public static Func>> ChacheTableEntityFactory = null; + private static ConcurrentDictionary> __cacheGetTableByEntity = new ConcurrentDictionary>(); + public static ConcurrentDictionary> _cacheGetTableByEntity + { + get + { + if (ChacheTableEntityFactory != null) + { + return ChacheTableEntityFactory.Invoke(); + } + return __cacheGetTableByEntity; + } + } internal static void RemoveTableByEntity(Type entity, CommonUtils common) { if (entity.IsAnonymousType() || @@ -534,7 +548,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 @@ -1780,16 +1794,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)),