diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs b/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs index d5a1a1462..6f74fa6b7 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs @@ -10,10 +10,11 @@ namespace SqlSugar protected List GetEntityList(SqlSugarClient context, IDataReader dataReader, string fields) { Type type = typeof(T); - string key = "DataReaderToList." + fields+ dataReader.FieldCount+ context.CurrentConnectionConfig.DbType + type.FullName; - IDataReaderEntityBuilder entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(key, () => + var fieldNames = GetDataReaderNames(dataReader); + string cacheKey = GetCacheKey(type,fieldNames); + IDataReaderEntityBuilder entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () => { - var cacheResult = new IDataReaderEntityBuilder(context, dataReader).CreateBuilder(type); + var cacheResult = new IDataReaderEntityBuilder(context, dataReader,fieldNames).CreateBuilder(type); return cacheResult; }); List result = new List(); @@ -32,6 +33,29 @@ namespace SqlSugar return result; } + private string GetCacheKey(Type type,List keys) + { + StringBuilder sb = new StringBuilder("DataReaderToList."); + sb.Append(type.FullName); + sb.Append("."); + foreach (var item in keys) + { + sb.Append(item); + } + return sb.ToString(); + } + + private List GetDataReaderNames(IDataReader dataReader) + { + List keys = new List(); + var count = dataReader.FieldCount; + for (int i = 0; i < count; i++) + { + keys.Add(dataReader.GetName(i)); + } + return keys; + } + protected List GetKeyValueList(Type type, IDataReader dataReader) { List reval = new List(); @@ -125,7 +149,7 @@ namespace SqlSugar } else if (type.IsEnum) { - reval.Add((T)Enum.Parse(type,value.ObjToString())); + reval.Add((T)Enum.Parse(type, value.ObjToString())); } else { diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs index 68168d8b6..918437e1e 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs @@ -74,12 +74,12 @@ namespace SqlSugar } - public IDataReaderEntityBuilder(SqlSugarClient context, IDataRecord dataRecord) + public IDataReaderEntityBuilder(SqlSugarClient context, IDataRecord dataRecord,List fieldNames) { this.Context = context; this.DataRecord = dataRecord; this.DynamicBuilder = new IDataReaderEntityBuilder(); - this.ReaderKeys = new List(); + this.ReaderKeys = fieldNames; } #endregion @@ -91,10 +91,6 @@ namespace SqlSugar public IDataReaderEntityBuilder CreateBuilder(Type type) { - for (int i = 0; i < this.DataRecord.FieldCount; i++) - { - this.ReaderKeys.Add(this.DataRecord.GetName(i)); - } DynamicMethod method = new DynamicMethod("SqlSugarEntity", type, new Type[] { typeof(IDataRecord) }, type, true); ILGenerator generator = method.GetILGenerator();