mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	- 优化 实体类重写属性 new 如果类型与基类不一致,无法使用的问题;
This commit is contained in:
		@@ -99,13 +99,6 @@
 | 
				
			|||||||
            清空状态数据
 | 
					            清空状态数据
 | 
				
			||||||
            </summary>
 | 
					            </summary>
 | 
				
			||||||
        </member>
 | 
					        </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)">
 | 
					        <member name="M:FreeSql.DbSet`1.Add(`0)">
 | 
				
			||||||
            <summary>
 | 
					            <summary>
 | 
				
			||||||
            添加
 | 
					            添加
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -418,9 +418,33 @@ namespace FreeSql.Tests
 | 
				
			|||||||
        public enum TestAddEnumType { 中国人, 日本人 }
 | 
					        public enum TestAddEnumType { 中国人, 日本人 }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public static AsyncLocal<Guid> TenrantId { get; set; } = new AsyncLocal<Guid>();
 | 
					        public static AsyncLocal<Guid> TenrantId { get; set; } = new AsyncLocal<Guid>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public class TestAddEnumEx : TestAddEnum
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            public new int Id { get; set; }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void Test1()
 | 
					        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) =>
 | 
					            //g.mysql.Aop.AuditValue += (_, e) =>
 | 
				
			||||||
            //{
 | 
					            //{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,7 +12,7 @@ namespace FreeSql.DataAnnotations
 | 
				
			|||||||
        public TableFluent(Type entityType, TableAttribute table)
 | 
					        public TableFluent(Type entityType, TableAttribute table)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _entityType = entityType;
 | 
					            _entityType = entityType;
 | 
				
			||||||
            _properties = _entityType.GetProperties().ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase);
 | 
					            _properties = _entityType.GetPropertiesDictIgnoreCase();
 | 
				
			||||||
            _table = table;
 | 
					            _table = table;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -43,6 +43,19 @@ public static partial class FreeSqlGlobalExtensions
 | 
				
			|||||||
    public static Type NullableTypeOrThis(this Type that) => that?.IsNullableType() == true ? that.GetGenericArguments().First() : that;
 | 
					    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));
 | 
					    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>
 | 
				
			||||||
    /// 测量两个经纬度的距离,返回单位:米
 | 
					    /// 测量两个经纬度的距离,返回单位:米
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,7 +23,6 @@ namespace FreeSql.Internal
 | 
				
			|||||||
            _common = common;
 | 
					            _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)
 | 
					        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 };
 | 
					            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;
 | 
					                    parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    //dto 映射
 | 
					                    //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 dtoProp in dtoProps)
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        foreach (var dtTb in _tables)
 | 
					                        foreach (var dtTb in _tables)
 | 
				
			||||||
@@ -180,7 +179,7 @@ namespace FreeSql.Internal
 | 
				
			|||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        //dto 映射
 | 
					                        //dto 映射
 | 
				
			||||||
                        parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;
 | 
					                        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 dtoProp in dtoProps2)
 | 
				
			||||||
                        {
 | 
					                        {
 | 
				
			||||||
                            foreach (var dtTb in _tables)
 | 
					                            foreach (var dtTb in _tables)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -79,11 +79,10 @@ namespace FreeSql.Internal.CommonProvider
 | 
				
			|||||||
            if (isThrowException) throw e;
 | 
					            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)
 | 
					        internal Dictionary<string, PropertyInfo> GetQueryTypeProperties(Type type)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            var tb = _util.GetTableByEntity(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;
 | 
					            return props;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        public List<T> Query<T>(string cmdText, object parms = null) => Query<T>(null, null, CommandType.Text, cmdText, GetDbParamtersByObject(cmdText, parms));
 | 
					        public List<T> Query<T>(string cmdText, object parms = null) => Query<T>(null, null, CommandType.Text, cmdText, GetDbParamtersByObject(cmdText, parms));
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -262,7 +262,7 @@ namespace FreeSql.Internal
 | 
				
			|||||||
            else
 | 
					            else
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                var sb = new StringBuilder();
 | 
					                var sb = new StringBuilder();
 | 
				
			||||||
                var ps = type.GetProperties();
 | 
					                var ps = type.GetPropertiesDictIgnoreCase().Values;
 | 
				
			||||||
                var psidx = 0;
 | 
					                var psidx = 0;
 | 
				
			||||||
                foreach (var p in ps)
 | 
					                foreach (var p in ps)
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
@@ -360,7 +360,7 @@ namespace FreeSql.Internal
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
                var xmlNav = xpath.CreateNavigator();
 | 
					                var xmlNav = xpath.CreateNavigator();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                var props = type.GetProperties();
 | 
					                var props = type.GetPropertiesDictIgnoreCase().Values;
 | 
				
			||||||
                foreach (var prop in props)
 | 
					                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('.');
 | 
					                    var className = (prop.DeclaringType.IsNested ? $"{prop.DeclaringType.Namespace}.{prop.DeclaringType.DeclaringType.Name}.{prop.DeclaringType.Name}" : $"{prop.DeclaringType.Namespace}.{prop.DeclaringType.Name}").Trim('.');
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -51,7 +51,7 @@ namespace FreeSql.Internal
 | 
				
			|||||||
            var tbattr = common.GetEntityTableAttribute(entity);
 | 
					            var tbattr = common.GetEntityTableAttribute(entity);
 | 
				
			||||||
            trytb = new TableInfo();
 | 
					            trytb = new TableInfo();
 | 
				
			||||||
            trytb.Type = entity;
 | 
					            trytb.Type = entity;
 | 
				
			||||||
            trytb.Properties = entity.GetProperties().ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase);
 | 
					            trytb.Properties = entity.GetPropertiesDictIgnoreCase();
 | 
				
			||||||
            trytb.CsName = entity.Name;
 | 
					            trytb.CsName = entity.Name;
 | 
				
			||||||
            trytb.DbName = (tbattr?.Name ?? entity.Name);
 | 
					            trytb.DbName = (tbattr?.Name ?? entity.Name);
 | 
				
			||||||
            trytb.DbOldName = tbattr?.OldName;
 | 
					            trytb.DbOldName = tbattr?.OldName;
 | 
				
			||||||
@@ -72,7 +72,7 @@ namespace FreeSql.Internal
 | 
				
			|||||||
            var columnsList = new List<ColumnInfo>();
 | 
					            var columnsList = new List<ColumnInfo>();
 | 
				
			||||||
            foreach (var p in trytb.Properties.Values)
 | 
					            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 colattr = common.GetEntityColumnAttribute(entity, p);
 | 
				
			||||||
                var tp = common.CodeFirst.GetDbInfo(colattr?.MapType ?? p.PropertyType);
 | 
					                var tp = common.CodeFirst.GetDbInfo(colattr?.MapType ?? p.PropertyType);
 | 
				
			||||||
                if (setMethod == null || (tp == null && p.PropertyType.IsValueType)) // 属性没有 set自动忽略
 | 
					                if (setMethod == null || (tp == null && p.PropertyType.IsValueType)) // 属性没有 set自动忽略
 | 
				
			||||||
@@ -391,7 +391,7 @@ namespace FreeSql.Internal
 | 
				
			|||||||
                {
 | 
					                {
 | 
				
			||||||
                    if (midType != null)
 | 
					                    if (midType != null)
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        var midTypeProps = midType.GetProperties();
 | 
					                        var midTypeProps = midType.GetPropertiesDictIgnoreCase().Values;
 | 
				
			||||||
                        var midTypePropsTrytb = midTypeProps.Where(a => a.PropertyType == trytb.Type).Count();
 | 
					                        var midTypePropsTrytb = midTypeProps.Where(a => a.PropertyType == trytb.Type).Count();
 | 
				
			||||||
                        var midTypePropsTbref = midTypeProps.Where(a => a.PropertyType == tbref.Type).Count();
 | 
					                        var midTypePropsTbref = midTypeProps.Where(a => a.PropertyType == tbref.Type).Count();
 | 
				
			||||||
                        if (midTypePropsTrytb != 1 || midTypePropsTbref != 1) midType = null;
 | 
					                        if (midTypePropsTrytb != 1 || midTypePropsTbref != 1) midType = null;
 | 
				
			||||||
@@ -1024,7 +1024,7 @@ namespace FreeSql.Internal
 | 
				
			|||||||
            var type = obj.GetType();
 | 
					            var type = obj.GetType();
 | 
				
			||||||
            if (type == ttype) return new[] { (T)Convert.ChangeType(obj, type) };
 | 
					            if (type == ttype) return new[] { (T)Convert.ChangeType(obj, type) };
 | 
				
			||||||
            var ret = new List<T>();
 | 
					            var ret = new List<T>();
 | 
				
			||||||
            var ps = type.GetProperties();
 | 
					            var ps = type.GetPropertiesDictIgnoreCase().Values;
 | 
				
			||||||
            foreach (var p in ps)
 | 
					            foreach (var p in ps)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                if (string.IsNullOrEmpty(paramPrefix) == false && sql.IndexOf($"{paramPrefix}{p.Name}", StringComparison.CurrentCultureIgnoreCase) == -1) continue;
 | 
					                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))
 | 
					                        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;
 | 
					                        var propIndex = 0;
 | 
				
			||||||
                        foreach (var prop in props)
 | 
					                        foreach (var prop in props)
 | 
				
			||||||
                        {
 | 
					                        {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user