- 优化 实体类重写属性 new 如果类型与基类不一致,无法使用的问题;

This commit is contained in:
28810 2019-11-10 12:18:16 +08:00
parent 5ce037d316
commit 8a9a50ecb7
8 changed files with 48 additions and 20 deletions

View File

@ -99,13 +99,6 @@
清空状态数据
</summary>
</member>
<member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
<summary>
根据 lambda 条件删除数据
</summary>
<param name="predicate"></param>
<returns></returns>
</member>
<member name="M:FreeSql.DbSet`1.Add(`0)">
<summary>
添加

View File

@ -418,9 +418,33 @@ namespace FreeSql.Tests
public enum TestAddEnumType { , }
public static AsyncLocal<Guid> TenrantId { get; set; } = new AsyncLocal<Guid>();
public class TestAddEnumEx : TestAddEnum
{
public new int Id { get; set; }
}
[Fact]
public void Test1()
{
var testExNewRet1 = g.sqlite.Delete<TestAddEnumEx>().Where("1=1").ExecuteAffrows();
var testExNewRet2 = g.sqlite.Insert<TestAddEnumEx>(new TestAddEnumEx { Id = 1, Type = TestAddEnumType. }).ExecuteAffrows();
var testExNewRet3 = g.sqlite.Insert<TestAddEnumEx>(new TestAddEnumEx { Id = 2, Type = TestAddEnumType. }).ExecuteAffrows();
var testExNewRet4 = g.sqlite.Select<TestAddEnumEx>().ToList();
var testExNewRet5 = g.sqlite.Update<TestAddEnumEx>(1).Set(a => a.Type == TestAddEnumType.).ExecuteAffrows();
var testExNewRet6 = g.sqlite.Select<TestAddEnumEx>().ToList();
var testExNewRet7 = g.sqlite.Delete<TestAddEnumEx>().Where("1=1").ExecuteAffrows();
var testExNewRet8 = g.sqlite.Select<TestAddEnumEx>().ToList();
var testBaseRet1 = g.sqlite.Delete<TestAddEnum>().Where("1=1").ExecuteAffrows();
var testBaseRet2 = g.sqlite.Insert<TestAddEnum>(new TestAddEnum { Type = TestAddEnumType. }).ExecuteAffrows();
var testBaseRet3 = g.sqlite.Insert<TestAddEnum>(new TestAddEnum { Type = TestAddEnumType. }).ExecuteAffrows();
var testBaseRet4 = g.sqlite.Select<TestAddEnum>().ToList();
var testBaseRet5 = g.sqlite.Update<TestAddEnum>(testBaseRet4[0]).Set(a => a.Type == TestAddEnumType.).ExecuteAffrows();
var testBaseRet6 = g.sqlite.Select<TestAddEnum>().ToList();
var testBaseRet7 = g.sqlite.Delete<TestAddEnum>().Where("1=1").ExecuteAffrows();
var testBaseRet8 = g.sqlite.Select<TestAddEnum>().ToList();
//g.mysql.Aop.AuditValue += (_, e) =>
//{

View File

@ -12,7 +12,7 @@ namespace FreeSql.DataAnnotations
public TableFluent(Type entityType, TableAttribute table)
{
_entityType = entityType;
_properties = _entityType.GetProperties().ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase);
_properties = _entityType.GetPropertiesDictIgnoreCase();
_table = table;
}

View File

@ -43,6 +43,19 @@ public static partial class FreeSqlGlobalExtensions
public static Type NullableTypeOrThis(this Type that) => that?.IsNullableType() == true ? that.GetGenericArguments().First() : that;
internal static string NotNullAndConcat(this string that, params object[] args) => string.IsNullOrEmpty(that) ? null : string.Concat(new object[] { that }.Concat(args));
static ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>> _dicGetPropertiesDictIgnoreCase = new ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>>();
public static Dictionary<string, PropertyInfo> GetPropertiesDictIgnoreCase(this Type that) => that == null ? null : _dicGetPropertiesDictIgnoreCase.GetOrAdd(that, tp =>
{
var props = that.GetProperties();
var dict = new Dictionary<string, PropertyInfo>(StringComparer.CurrentCultureIgnoreCase);
foreach (var prop in props)
{
if (dict.ContainsKey(prop.Name)) continue;
dict.Add(prop.Name, prop);
}
return dict;
});
/// <summary>
/// 测量两个经纬度的距离,返回单位:米
/// </summary>

View File

@ -23,7 +23,6 @@ namespace FreeSql.Internal
_common = common;
}
static ConcurrentDictionary<Type, PropertyInfo[]> _dicReadAnonymousFieldDtoPropertys = new ConcurrentDictionary<Type, PropertyInfo[]>();
public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder field, ReadAnonymousTypeInfo parent, ref int index, Expression exp, Func<Expression[], string> getSelectGroupingMapString, List<LambdaExpression> whereCascadeExpression)
{
Func<ExpTSC> getTSC = () => new ExpTSC { _tables = _tables, getSelectGroupingMapString = getSelectGroupingMapString, tbtype = SelectTableInfoType.From, isQuoteName = true, isDisableDiyParse = false, style = ExpressionStyle.Where, whereCascadeExpression = whereCascadeExpression };
@ -110,7 +109,7 @@ namespace FreeSql.Internal
parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;
//dto 映射
var dtoProps = _dicReadAnonymousFieldDtoPropertys.GetOrAdd(initExp.NewExpression.Type, dtoType => dtoType.GetProperties());
var dtoProps = initExp.NewExpression.Type.GetPropertiesDictIgnoreCase().Values;
foreach (var dtoProp in dtoProps)
{
foreach (var dtTb in _tables)
@ -180,7 +179,7 @@ namespace FreeSql.Internal
{
//dto 映射
parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;
var dtoProps2 = _dicReadAnonymousFieldDtoPropertys.GetOrAdd(newExp.Type, dtoType => dtoType.GetProperties());
var dtoProps2 = newExp.Type.GetPropertiesDictIgnoreCase().Values;
foreach (var dtoProp in dtoProps2)
{
foreach (var dtTb in _tables)

View File

@ -79,11 +79,10 @@ namespace FreeSql.Internal.CommonProvider
if (isThrowException) throw e;
}
internal static ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>> dicQueryTypeGetProperties = new ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>>();
internal Dictionary<string, PropertyInfo> GetQueryTypeProperties(Type type)
{
var tb = _util.GetTableByEntity(type);
var props = tb?.Properties ?? dicQueryTypeGetProperties.GetOrAdd(type, k => type.GetProperties().ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase));
var props = tb?.Properties ?? type.GetPropertiesDictIgnoreCase();
return props;
}
public List<T> Query<T>(string cmdText, object parms = null) => Query<T>(null, null, CommandType.Text, cmdText, GetDbParamtersByObject(cmdText, parms));

View File

@ -262,7 +262,7 @@ namespace FreeSql.Internal
else
{
var sb = new StringBuilder();
var ps = type.GetProperties();
var ps = type.GetPropertiesDictIgnoreCase().Values;
var psidx = 0;
foreach (var p in ps)
{
@ -360,7 +360,7 @@ namespace FreeSql.Internal
}
var xmlNav = xpath.CreateNavigator();
var props = type.GetProperties();
var props = type.GetPropertiesDictIgnoreCase().Values;
foreach (var prop in props)
{
var className = (prop.DeclaringType.IsNested ? $"{prop.DeclaringType.Namespace}.{prop.DeclaringType.DeclaringType.Name}.{prop.DeclaringType.Name}" : $"{prop.DeclaringType.Namespace}.{prop.DeclaringType.Name}").Trim('.');

View File

@ -51,7 +51,7 @@ namespace FreeSql.Internal
var tbattr = common.GetEntityTableAttribute(entity);
trytb = new TableInfo();
trytb.Type = entity;
trytb.Properties = entity.GetProperties().ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase);
trytb.Properties = entity.GetPropertiesDictIgnoreCase();
trytb.CsName = entity.Name;
trytb.DbName = (tbattr?.Name ?? entity.Name);
trytb.DbOldName = tbattr?.OldName;
@ -72,7 +72,7 @@ namespace FreeSql.Internal
var columnsList = new List<ColumnInfo>();
foreach (var p in trytb.Properties.Values)
{
var setMethod = trytb.Type.GetMethod($"set_{p.Name}");
var setMethod = p.GetSetMethod(); //trytb.Type.GetMethod($"set_{p.Name}");
var colattr = common.GetEntityColumnAttribute(entity, p);
var tp = common.CodeFirst.GetDbInfo(colattr?.MapType ?? p.PropertyType);
if (setMethod == null || (tp == null && p.PropertyType.IsValueType)) // 属性没有 set自动忽略
@ -391,7 +391,7 @@ namespace FreeSql.Internal
{
if (midType != null)
{
var midTypeProps = midType.GetProperties();
var midTypeProps = midType.GetPropertiesDictIgnoreCase().Values;
var midTypePropsTrytb = midTypeProps.Where(a => a.PropertyType == trytb.Type).Count();
var midTypePropsTbref = midTypeProps.Where(a => a.PropertyType == tbref.Type).Count();
if (midTypePropsTrytb != 1 || midTypePropsTbref != 1) midType = null;
@ -1024,7 +1024,7 @@ namespace FreeSql.Internal
var type = obj.GetType();
if (type == ttype) return new[] { (T)Convert.ChangeType(obj, type) };
var ret = new List<T>();
var ps = type.GetProperties();
var ps = type.GetPropertiesDictIgnoreCase().Values;
foreach (var p in ps)
{
if (string.IsNullOrEmpty(paramPrefix) == false && sql.IndexOf($"{paramPrefix}{p.Name}", StringComparison.CurrentCultureIgnoreCase) == -1) continue;
@ -1346,7 +1346,7 @@ namespace FreeSql.Internal
Expression.Assign(readpknullExp, Expression.Constant(false))
});
var props = type.GetProperties();//.ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase);
var props = type.GetPropertiesDictIgnoreCase().Values;
var propIndex = 0;
foreach (var prop in props)
{