diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index dc6d19ef8..791d92cf0 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -620,14 +620,19 @@ namespace SqlSugar foreach (var column in EntityInfo.Columns) { if (column.IsIgnore || column.IsOnlyIgnoreInsert) continue; + var isMapping = IsMappingColumns; var columnInfo = new DbColumnInfo() { - Value = column.PropertyInfo.GetValue(item, null), - DbColumnName = GetDbColumnName(column.PropertyName), + Value = PropertyCallAdapterProvider.GetInstance(column.PropertyName).InvokeGet(item), + DbColumnName = column.DbColumnName, PropertyName = column.PropertyName, PropertyType = UtilMethods.GetUnderType(column.PropertyInfo), TableId = i }; + if (isMapping) + { + columnInfo.DbColumnName = GetDbColumnName(column.PropertyName); + } if (column.IsJson) { columnInfo.IsJson = true; @@ -645,8 +650,8 @@ namespace SqlSugar if(columnInfo.Value!=null) columnInfo.Value = this.Context.Utilities.SerializeObject(columnInfo.Value); } - var tranColumn=EntityInfo.Columns.FirstOrDefault(it => it.IsTranscoding && it.DbColumnName.Equals(column.DbColumnName, StringComparison.CurrentCultureIgnoreCase)); - if (tranColumn!=null&&columnInfo.Value.HasValue()) { + //var tranColumn=EntityInfo.Columns.FirstOrDefault(it => it.IsTranscoding && it.DbColumnName.Equals(column.DbColumnName, StringComparison.CurrentCultureIgnoreCase)); + if (column.IsTranscoding&&columnInfo.Value.HasValue()) { columnInfo.Value = UtilMethods.EncodeBase64(columnInfo.Value.ToString()); } insertItem.Add(columnInfo); diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerBlukCopy.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerBlukCopy.cs index bfdd63280..3881baa82 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerBlukCopy.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerBlukCopy.cs @@ -123,28 +123,20 @@ namespace SqlSugar foreach (var rowInfos in DbColumnInfoList) { var dr = dt.NewRow(); - foreach (DataColumn item in dt.Columns) + foreach (var value in rowInfos) { - var rows = rowInfos.ToList(); - var value = rows.FirstOrDefault(it => - it.DbColumnName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase) || - it.PropertyName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase) - ); - if (value != null) + if (value.Value != null && UtilMethods.GetUnderType(value.Value.GetType()) == UtilConstants.DateType) { - if (value.Value != null && UtilMethods.GetUnderType(value.Value.GetType()) == UtilConstants.DateType) + if (value.Value != null && value.Value.ToString() == DateTime.MinValue.ToString()) { - if (value.Value != null && value.Value.ToString() == DateTime.MinValue.ToString()) - { - value.Value = Convert.ToDateTime("1753/01/01"); - } + value.Value = Convert.ToDateTime("1753/01/01"); } - if (value.Value == null) - { - value.Value = DBNull.Value; - } - dr[item.ColumnName] = value.Value; } + if (value.Value == null) + { + value.Value = DBNull.Value; + } + dr[value.DbColumnName] = value.Value; } dt.Rows.Add(dr); } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Utilities/PropertyCallAdapterProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Utilities/PropertyCallAdapterProvider.cs new file mode 100644 index 000000000..b6acb8fc1 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Utilities/PropertyCallAdapterProvider.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Reflection; +namespace SqlSugar +{ + public interface IPropertyCallAdapter + { + object InvokeGet(TThis @this); + //add void InvokeSet(TThis @this, object value) if necessary + } + public class PropertyCallAdapter : IPropertyCallAdapter + { + private readonly Func _getterInvocation; + + public PropertyCallAdapter(Func getterInvocation) + { + _getterInvocation = getterInvocation; + } + + public object InvokeGet(TThis @this) + { + return _getterInvocation.Invoke(@this); + } + } + public class PropertyCallAdapterProvider + { + private static readonly Dictionary> _instances = + new Dictionary>(); + + public static IPropertyCallAdapter GetInstance(string forPropertyName) + { + IPropertyCallAdapter instance; + if (!_instances.TryGetValue(forPropertyName, out instance)) + { + var property = typeof(TThis).GetProperty( + forPropertyName, + BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + + MethodInfo getMethod; + Delegate getterInvocation = null; + if (property != null && (getMethod = property.GetGetMethod(true)) != null) + { + var openGetterType = typeof(Func<,>); + var concreteGetterType = openGetterType + .MakeGenericType(typeof(TThis), property.PropertyType); + + getterInvocation = + Delegate.CreateDelegate(concreteGetterType, null, getMethod); + } + else + { + //throw exception or create a default getterInvocation returning null + } + + var openAdapterType = typeof(PropertyCallAdapter<,>); + var concreteAdapterType = openAdapterType + .MakeGenericType(typeof(TThis), property.PropertyType); + instance = Activator + .CreateInstance(concreteAdapterType, getterInvocation) + as IPropertyCallAdapter; + + _instances.Add(forPropertyName, instance); + } + + return instance; + } + } +} \ No newline at end of file