- 增加 实体备注从 Description/Display/DisplayName 等特性兼容读取;

This commit is contained in:
2881099 2023-03-02 18:25:40 +08:00
parent 7c12b546b3
commit 4a4751708a
3 changed files with 23 additions and 33 deletions

View File

@ -569,6 +569,8 @@ namespace base_entity
#endregion #endregion
fsql.UseJsonMap(); fsql.UseJsonMap();
var displayNameTb = fsql.CodeFirst.GetTableByEntity(typeof(DeviceCodes));
var joinsql1 = fsql.Select<JoinTest01>() var joinsql1 = fsql.Select<JoinTest01>()
.Include(a => a.Parent.Parent) .Include(a => a.Parent.Parent)
.Where(a => a.Parent.Parent.code == "001") .Where(a => a.Parent.Parent.code == "001")

View File

@ -733,15 +733,6 @@
<param name="modelBuilder"></param> <param name="modelBuilder"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSqlDbContextExtensions.ApplyConfigurationsFromAssembly(FreeSql.ICodeFirst,System.Reflection.Assembly,System.Func{System.Type,System.Boolean})">
<summary>
根据Assembly扫描所有继承IEntityTypeConfiguration&lt;T&gt;的配置类
</summary>
<param name="codeFirst"></param>
<param name="assembly"></param>
<param name="predicate"></param>
<returns></returns>
</member>
<member name="M:FreeSqlDbContextExtensions.CreateDbContext(IFreeSql)"> <member name="M:FreeSqlDbContextExtensions.CreateDbContext(IFreeSql)">
<summary> <summary>
创建普通数据上下文档对象 创建普通数据上下文档对象
@ -800,14 +791,5 @@
<param name="that"></param> <param name="that"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
<summary>
批量注入 Repository可以参考代码自行调整
</summary>
<param name="services"></param>
<param name="globalDataFilter"></param>
<param name="assemblies"></param>
<returns></returns>
</member>
</members> </members>
</doc> </doc>

View File

@ -1,6 +1,5 @@
using FreeSql.DataAnnotations; using FreeSql.DataAnnotations;
using FreeSql.DatabaseModel; using FreeSql.DatabaseModel;
using FreeSql.Extensions.EntityUtil;
using FreeSql.Internal.Model; using FreeSql.Internal.Model;
using FreeSql.Internal.ObjectPool; using FreeSql.Internal.ObjectPool;
using System; using System;
@ -10,7 +9,6 @@ using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -607,6 +605,21 @@ namespace FreeSql.Internal
return dic; 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) void GetDydesc(PropertyInfo prop)
{ {
object[] attrs = null; object[] attrs = null;
@ -617,19 +630,12 @@ namespace FreeSql.Internal
prop.GetCustomAttributes(false).ToArray(); //.net core 反射存在版本冲突问题,导致该方法异常 prop.GetCustomAttributes(false).ToArray(); //.net core 反射存在版本冲突问题,导致该方法异常
} }
catch { } catch { }
var comment = GetByAttribute(attrs, "DescriptionAttribute");
var dyattr = attrs?.Where(a => { if (string.IsNullOrEmpty(comment)) comment = GetByAttribute(attrs, "DisplayNameAttribute");
return ((a as Attribute)?.TypeId as Type)?.Name == "DescriptionAttribute"; if (string.IsNullOrEmpty(comment)) comment = GetByAttribute(attrs, "DisplayAttribute");
}).FirstOrDefault(); if (string.IsNullOrEmpty(comment)) comment = GetByAttribute(attrs, "DisplayColumnAttribute");
if (dyattr != null) if (string.IsNullOrEmpty(comment) == false)
{ dic.Add(prop == null ? "" : prop.Name, comment);
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);
}
} }
} }