最近在项目中需要把各个字段的释义写到数据库中,该项目已经上线很长时间了,数据库中的字段没有上千也有上百个,要是一个项目一个项目打开然后再去找对应字段查看什么意思,估计要到明年过年了。由于项目中使用EntityFramework,本身这个项目只有手动设置字段注释的功能,Coder平时写代码的时候都懒得写注释,更别提能在配置数据库的时候将注释配置进去,所以如何在EF中自动将实体注释写入数据库,减轻Coder的压力(ru he tou lan)尤为重要。gitee地址:https://gitee.com/lbqman/Blog20210206.git 。下面进入正题。
/// <summary>Configures a comment to be applied to the column</summary>
/// <typeparam name="TProperty"> The type of the property being configured. </typeparam>
/// <param name="propertyBuilder"> The builder for the property being configured. </param>
/// <param name="comment"> The comment for the column. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static PropertyBuilder<TProperty> HasComment<TProperty>(
[NotNull] this PropertyBuilder<TProperty> propertyBuilder,
[CanBeNull] string comment)
{
return (PropertyBuilder<TProperty>) propertyBuilder.HasComment(comment);
}
public virtual PropertyBuilder<TProperty> Property<TProperty>(
[NotNull] Expression<Func<TEntity, TProperty>> propertyExpression);
public static PropertyBuilder<TProperty> SummaryProperty<TEntity, TProperty>(
this EntityTypeBuilder<TEntity> entityTypeBuilder,
Expression<Func<TEntity, TProperty>> propertyExpression)
where TEntity : class
public static MemberInfo GetMember<T, TProperty>(this Expression<Func<T, TProperty>> expression)
{
MemberExpression memberExp;
if (expression.Body is UnaryExpression unaryExpression)
memberExp = unaryExpression.Operand as MemberExpression;
else
memberExp = expression.Body as MemberExpression;
return memberExp?.Member;
}
/// <summary>
/// xml注释获取器
/// </summary>
internal static class SummaryXmlCacheProvider
{
#region TClass
/// <summary>
/// 根据类型初始化该类所在程序集的xml
/// </summary>
/// <typeparam name="TClass"></typeparam>
internal static void InitSummaryXml<TClass>()
{
var assembly = Assembly.GetAssembly(typeof(TClass));
SerializeXmlFromAssembly(assembly);
}
/// <summary>
/// 根据类型获取该类所在程序集的xml
/// </summary>
/// <typeparam name="TClass"></typeparam>
/// <returns></returns>
internal static Dictionary<string, string> GetSummaryXml<TClass>()
{
var assembly = Assembly.GetAssembly(typeof(TClass));
return SummaryCache[assembly];
}
/// <summary>
/// 获取该类在xml的key
/// </summary>
/// <typeparam name="TClass"></typeparam>
/// <returns></returns>
internal static string GetClassTypeKey<TClass>()
{
return TableSummaryRuleProvider.TypeSummaryKey(typeof(TClass).FullName);
}
#endregion
#region TProperty
/// <summary>
/// 根据类型以及字段初始化该类所在程序集的xml
/// </summary>
/// <typeparam name="TClass"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="propertyExpression"></param>
internal static void InitSummaryXml<TClass, TProperty>(Expression<Func<TClass, TProperty>> propertyExpression)
{
var propertyAssembly = GetPropertyAssembly(propertyExpression);
SerializeXmlFromAssembly(propertyAssembly);
}
/// <summary>
/// 根据类型以及字段获取该类所在程序集的xml
/// </summary>
/// <typeparam name="TClass"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="propertyExpression"></param>
/// <returns></returns>
internal static Dictionary<string, string> GetSummaryXml<TClass, TProperty>(
Expression<Func<TClass, TProperty>> propertyExpression)
{
var propertyAssembly = GetPropertyAssembly(propertyExpression);
return SummaryCache[propertyAssembly];
}
/// <summary>
/// 获取该类以及字段所在xml的key
/// </summary>
/// <typeparam name="TClass"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="propertyExpression"></param>
/// <returns></returns>
internal static string GetPropertyTypeKey<TClass, TProperty>(
Expression<Func<TClass, TProperty>> propertyExpression)
{
var memberName = propertyExpression.GetMember().Name;
var propertyInfo = GetPropertyInfo(propertyExpression);
var propertyKey =
$"{propertyInfo.DeclaringType.Namespace}.{propertyInfo.DeclaringType.Name}.{memberName}";
return PropertySummaryRuleProvider.PropertyTypeSummaryKey(propertyKey);
}
#endregion
#region TEnum
/// <summary>
/// 获取枚举字段的描述信息
/// </summary>
/// <typeparam name="TClass"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="propertyExpression"></param>
/// <returns></returns>
internal static string GetEnumPropertyDescription<TClass, TProperty>(Expression<Func<TClass, TProperty>> propertyExpression)
{
var propertyInfo = GetPropertyInfo(propertyExpression);
if (!propertyInfo.PropertyType.IsEnum)
return string.Empty;
var enumType = propertyInfo.PropertyType;
SerializeXmlFromAssembly(enumType.Assembly);
var propertySummaryDic = SummaryCache[enumType.Assembly];
var enumNames = enumType.GetEnumNames();
var enumDescDic = enumType.GetNameAndValues();
var enumSummaries = new List<string>();
foreach (var enumName in enumNames)
{
var propertyEnumKey = PropertySummaryRuleProvider.EnumTypeSummaryKey($"{enumType.FullName}.{enumName}");
var enumSummary = propertySummaryDic.ContainsKey(propertyEnumKey)
? propertySummaryDic[propertyEnumKey]
: string.Empty;
var enumValue = enumDescDic[enumName];
enumSummaries.Add(PropertySummaryRuleProvider.EnumTypeSummaryFormat(enumValue,enumName,enumSummary));
}
return string.Join(";", enumSummaries);
}
#endregion
/// <summary>
/// 根据表达式获取属性所在的程序集
/// </summary>
/// <typeparam name="TClass"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="propertyExpression"></param>
/// <returns></returns>
private static Assembly GetPropertyAssembly<TClass, TProperty>(
Expression<Func<TClass, TProperty>> propertyExpression)
{
var propertyInfo = GetPropertyInfo(propertyExpression);
var propertyAssembly = propertyInfo.Module.Assembly;
return propertyAssembly;
}
/// <summary>
/// 根据表达式获取字段属性
/// </summary>
/// <typeparam name="TClass"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="propertyExpression"></param>
/// <returns></returns>
private static PropertyInfo GetPropertyInfo<TClass, TProperty>(
Expression<Func<TClass, TProperty>> propertyExpression)
{
var entityType = typeof(TClass);
var memberName = propertyExpression.GetMember().Name;
var propertyInfo = entityType.GetProperty(memberName, typeof(TProperty));
if (propertyInfo == null || propertyInfo.DeclaringType == null)
throw new ArgumentNullException($"this property {memberName} is not belong to {entityType.Name}");
return propertyInfo;
}
/// <summary>
/// 根据程序集初始化xml
/// </summary>
/// <param name="assembly"></param>
private static void SerializeXmlFromAssembly(Assembly assembly)
{
var assemblyPath = assembly.Location;
var lastIndexOf = assemblyPath.LastIndexOf(".dll", StringComparison.Ordinal);
var xmlPath = assemblyPath.Remove(lastIndexOf, 4) + ".xml";
if (SummaryCache.ContainsKey(assembly))
return;
var xmlDic = new Dictionary<string, string>();
if (!File.Exists(xmlPath))
{
Console.WriteLine($"未能加载xml文件,原因:xml文件不存在,path:{xmlPath}");
SummaryCache.Add(assembly, xmlDic);
return;
}
var doc = new XmlDocument();
doc.Load(xmlPath);
var members = doc.SelectNodes("doc/members/member");
if (members == null)
{
Console.WriteLine($"未能加载xml文件,原因:doc/members/member节点不存在");
SummaryCache.Add(assembly, xmlDic);
return;
}
foreach (XmlElement member in members)
{
var name = member.Attributes["name"].InnerText.Trim();
if (string.IsNullOrWhiteSpace(name))
continue;
xmlDic.Add(name, member.SelectSingleNode("summary")?.InnerText.Trim());
}
SummaryCache.Add(assembly, xmlDic);
}
/// <summary>
/// xml注释缓存
/// </summary>
private static Dictionary<Assembly, Dictionary<string, string>> SummaryCache { get; } =
new Dictionary<Assembly, Dictionary<string, string>>();
}
public static PropertyBuilder<TProperty> SummaryProperty<TEntity, TProperty>(
this EntityTypeBuilder<TEntity> entityTypeBuilder,
Expression<Func<TEntity, TProperty>> propertyExpression)
where TEntity : class
{
SummaryXmlCacheProvider.InitSummaryXml(propertyExpression);
var entitySummaryDic = SummaryXmlCacheProvider.GetSummaryXml(propertyExpression);
var propertyKey = SummaryXmlCacheProvider.GetPropertyTypeKey(propertyExpression);
var summary = entitySummaryDic.ContainsKey(propertyKey) ? entitySummaryDic[propertyKey] : string.Empty;
var enumDescription = SummaryXmlCacheProvider.GetEnumPropertyDescription(propertyExpression);
summary = string.IsNullOrWhiteSpace(enumDescription) ? summary : $"{summary}:{enumDescription}";
return string.IsNullOrWhiteSpace(summary)
? entityTypeBuilder.Property(propertyExpression)
: entityTypeBuilder.Property(propertyExpression).HasComment(summary);
}
public static EntityTypeBuilder<TEntity> SummaryToTable<TEntity>(
this EntityTypeBuilder<TEntity> entityTypeBuilder, bool hasTableComment = true,
Func<string> tablePrefix = null)
where TEntity : class
{
var tableName = GetTableName<TEntity>(tablePrefix);
return hasTableComment
? entityTypeBuilder.ToTable(tableName)
.SummaryHasComment()
: entityTypeBuilder.ToTable(tableName);
}
public static EntityTypeBuilder<TEntity> SummaryHasComment<TEntity>(
this EntityTypeBuilder<TEntity> entityTypeBuilder) where TEntity : class
{
SummaryXmlCacheProvider.InitSummaryXml<TEntity>();
var entityDic = SummaryXmlCacheProvider.GetSummaryXml<TEntity>();
var tableKey = SummaryXmlCacheProvider.GetClassTypeKey<TEntity>();
var summary = entityDic.ContainsKey(tableKey) ? entityDic[tableKey] : string.Empty;
return string.IsNullOrWhiteSpace(summary) ? entityTypeBuilder : entityTypeBuilder.HasComment(summary);
}
private static string GetTableName<TEntity>(Func<string> tablePrefix)
{
return typeof(TEntity).Name.Replace("Entity", $"Tb{tablePrefix?.Invoke()}");
}
public partial class InitDb : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TbGood",
columns: table => new
{
Id = table.Column<long>(nullable: false, comment: "主键")
.Annotation("SqlServer:Identity", "1, 1"),
CreateTime = table.Column<DateTime>(nullable: false, comment: "创建时间"),
CreateName = table.Column<string>(maxLength: 64, nullable: false, comment: "创建人姓名"),
UpdateTime = table.Column<DateTime>(nullable: true, comment: "更新时间"),
UpdateName = table.Column<string>(maxLength: 64, nullable: true, comment: "更新人姓名"),
Name = table.Column<string>(maxLength: 64, nullable: false, comment: "商品名称"),
GoodType = table.Column<int>(nullable: false, comment: "物品类型:(0,Electronic) 电子产品;(1,Clothes) 衣帽服装;(2,Food) 食品;(3,Other) 其他物品"),
Description = table.Column<string>(maxLength: 2048, nullable: true, comment: "物品描述"),
Store = table.Column<int>(nullable: false, comment: "储存量")
},
constraints: table =>
{
table.PrimaryKey("PK_TbGood", x => x.Id);
},
comment: "商品实体类");
migrationBuilder.CreateTable(
name: "TbOrder",
columns: table => new
{
Id = table.Column<long>(nullable: false, comment: "主键")
.Annotation("SqlServer:Identity", "1, 1"),
CreateTime = table.Column<DateTime>(nullable: false, comment: "创建时间"),
CreateName = table.Column<string>(maxLength: 64, nullable: false, comment: "创建人姓名"),
UpdateTime = table.Column<DateTime>(nullable: true, comment: "更新时间"),
UpdateName = table.Column<string>(maxLength: 64, nullable: true, comment: "更新人姓名"),
GoodId = table.Column<long>(nullable: false, comment: "商品Id"),
OrderStatus = table.Column<int>(nullable: false, comment: "订单状态:(0,Ordered) 已下单;(1,Payed) 已付款;(2,Complete) 已付款;(3,Cancel) 已取消"),
OrderTime = table.Column<DateTime>(nullable: false, comment: "下订单时间"),
Address = table.Column<string>(maxLength: 2048, nullable: false, comment: "订单地址"),
UserName = table.Column<string>(maxLength: 16, nullable: false, comment: "收件人姓名"),
TotalAmount = table.Column<decimal>(nullable: false, comment: "总金额")
},
constraints: table =>
{
table.PrimaryKey("PK_TbOrder", x => x.Id);
},
comment: "订单实体类");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "TbGood");
migrationBuilder.DropTable(
name: "TbOrder");
}
}
原文:https://www.cnblogs.com/Lbqman/p/14381690.html