Skip to content

生成数据库时将属性[DisplayName]特性的值添加到表字段的描述信息中 #213

@gmf520

Description

@gmf520

您的功能请求与现有问题有关吗?请描述

生成数据库时将属性[DisplayName]特性的值添加到表字段的描述信息中

描述您想要的解决方案

实现

基于 #212 的实现,添加一个批量配置实现,使用HasComment配置实现此需求

    /// <summary>
    /// 给实体属性添加Comment配置
    /// 生成数据库时将属性[DisplayName]特性的值添加到表字段的描述信息中
    /// </summary>
    public class PropertyCommentConfiguration : IEntityBatchConfiguration
    {
        /// <summary>
        /// 配置指定的<see cref="IMutableEntityType"/>
        /// </summary>
        /// <param name="modelBuilder">模型构建器</param>
        /// <param name="mutableEntityType">实体的<see cref="IMutableEntityType"/>类型</param>
        public void Configure(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType)
        {
            IMutableProperty[] mutableProperties = mutableEntityType.GetProperties().ToArray();
            foreach (IMutableProperty mutableProperty in mutableProperties)
            {
                if (mutableProperty.PropertyInfo == null)
                {
                    continue;
                }

                string display = mutableProperty.PropertyInfo.GetDescription();
                modelBuilder.Entity(mutableEntityType.ClrType).Property(mutableProperty.PropertyInfo.Name).HasComment(display);
            }
        }
    }

加入DI启用此功能

services.AddSingleton<IEntityBatchConfiguration, PropertyCommentConfiguration>();

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions