dotnetcore / osharp

OSharp是一个基于.Net6.0的快速开发框架,框架对 AspNetCore 的配置、依赖注入、日志、缓存、实体框架、Mvc(WebApi)、身份认证、功能权限、数据权限等模块进行更高一级的自动化封装,并规范了一套业务实现的代码结构与操作流程,使 .Net 框架更易于应用到实际项目开发中。
Apache License 2.0
2.77k stars 748 forks source link

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

Closed gmf520 closed 3 years ago

gmf520 commented 3 years ago

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

生成数据库时将属性[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>();