忽略列名称大小写约定

This commit is contained in:
falcon 2019-11-25 13:51:52 +08:00
parent c06bec69a2
commit 66e6e549a1

View File

@ -5,7 +5,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
namespace Falcon.ModelSP namespace Falcon.ModelSP
{ {
@ -109,7 +109,7 @@ namespace Falcon.ModelSP
for(var i = 0;i < columnSchema.Count;i++) { for(var i = 0;i < columnSchema.Count;i++) {
var name = dr.GetName(i); var name = dr.GetName(i);
var value = dr.IsDBNull(i) ? null : dr.GetValue(i); var value = dr.IsDBNull(i) ? null : dr.GetValue(i);
var pi = typeof(TR).GetProperty(name); var pi = getProperty<TR>(name);
if(pi == null || !pi.CanWrite) if(pi == null || !pi.CanWrite)
continue; continue;
pi.SetValue(item,value); pi.SetValue(item,value);
@ -120,6 +120,17 @@ namespace Falcon.ModelSP
return result; return result;
} }
} }
/// <summary>
/// 忽略大小写获取类型的属性
/// </summary>
private static PropertyInfo getProperty<T>(string name) {
var type = typeof(T);
foreach(var item in type.GetProperties()) {
if(item.Name.ToLower() == name.ToLower()) {
return item;
}
}
return null;
}
} }
} }