From 4a4751708a621a61dc4e5571069c5a34f7a0a9ce Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Thu, 2 Mar 2023 18:25:40 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20=E5=AE=9E=E4=BD=93?= =?UTF-8?q?=E5=A4=87=E6=B3=A8=E4=BB=8E=20Description/Display/DisplayName?= =?UTF-8?q?=20=E7=AD=89=E7=89=B9=E6=80=A7=E5=85=BC=E5=AE=B9=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Examples/base_entity/Program.cs | 2 ++ FreeSql.DbContext/FreeSql.DbContext.xml | 18 ------------- FreeSql/Internal/CommonUtils.cs | 36 ++++++++++++++----------- 3 files changed, 23 insertions(+), 33 deletions(-) diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index a2859f7d..21f714be 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -569,6 +569,8 @@ namespace base_entity #endregion fsql.UseJsonMap(); + var displayNameTb = fsql.CodeFirst.GetTableByEntity(typeof(DeviceCodes)); + var joinsql1 = fsql.Select() .Include(a => a.Parent.Parent) .Where(a => a.Parent.Parent.code == "001") diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 26522f10..594fbad3 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -733,15 +733,6 @@ - - - 根据Assembly扫描所有继承IEntityTypeConfiguration<T>的配置类 - - - - - - 创建普通数据上下文档对象 @@ -800,14 +791,5 @@ - - - 批量注入 Repository,可以参考代码自行调整 - - - - - - diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs index 1e07f2e8..ca3198d1 100644 --- a/FreeSql/Internal/CommonUtils.cs +++ b/FreeSql/Internal/CommonUtils.cs @@ -1,6 +1,5 @@ using FreeSql.DataAnnotations; using FreeSql.DatabaseModel; -using FreeSql.Extensions.EntityUtil; using FreeSql.Internal.Model; using FreeSql.Internal.ObjectPool; using System; @@ -10,7 +9,6 @@ using System.Collections.Generic; using System.Data.Common; using System.IO; using System.Linq; -using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Text.RegularExpressions; @@ -607,6 +605,21 @@ namespace FreeSql.Internal return dic; + string GetByAttribute(object[] attrs, string attributeName) + { + var dyattr = attrs?.Where(a => { + return ((a as Attribute)?.TypeId as Type)?.Name == attributeName; + }).FirstOrDefault(); + if (dyattr == null) return null; + var dyattrProps = dyattr.GetType().GetProperties(); + return dyattrProps.Where(a => a.PropertyType == typeof(string) && a.Name == "Description").FirstOrDefault()?.GetValue(dyattr, null)?.ToString() ?? + dyattrProps.Where(a => a.PropertyType == typeof(string) && a.Name == "Name").FirstOrDefault()?.GetValue(dyattr, null)?.ToString() ?? + dyattrProps.Where(a => a.PropertyType == typeof(string) && a.Name == "ShortName").FirstOrDefault()?.GetValue(dyattr, null)?.ToString() ?? + dyattrProps.Where(a => a.PropertyType == typeof(string)) + .Select(a => a?.GetValue(dyattr, null)?.ToString()) + .Where(a => !string.IsNullOrWhiteSpace(a)) + .FirstOrDefault(); + } void GetDydesc(PropertyInfo prop) { object[] attrs = null; @@ -617,19 +630,12 @@ namespace FreeSql.Internal prop.GetCustomAttributes(false).ToArray(); //.net core 反射存在版本冲突问题,导致该方法异常 } catch { } - - var dyattr = attrs?.Where(a => { - return ((a as Attribute)?.TypeId as Type)?.Name == "DescriptionAttribute"; - }).FirstOrDefault(); - if (dyattr != null) - { - var valueProp = dyattr.GetType().GetProperties().Where(a => a.PropertyType == typeof(string)).FirstOrDefault(); - var comment = valueProp?.GetValue(dyattr, null)?.ToString(); - if (string.IsNullOrEmpty(comment) == false) - dic.Add(prop == null ? - "" : - prop.Name, comment); - } + var comment = GetByAttribute(attrs, "DescriptionAttribute"); + if (string.IsNullOrEmpty(comment)) comment = GetByAttribute(attrs, "DisplayNameAttribute"); + if (string.IsNullOrEmpty(comment)) comment = GetByAttribute(attrs, "DisplayAttribute"); + if (string.IsNullOrEmpty(comment)) comment = GetByAttribute(attrs, "DisplayColumnAttribute"); + if (string.IsNullOrEmpty(comment) == false) + dic.Add(prop == null ? "" : prop.Name, comment); } }