Abp框架,EF 生成迁移文件时,自动添加表和字段注释内容

在使用 abp 框架,或者ef 的时候都会遇到一个问题,就是建实体后要将实体描述生成到数据库中,就需要手动去添加

cs 复制代码
[Comment("注释内容")]

注解,这样相当于手动写两次注释(即使你是 Ctrl + C),这样不免有些麻烦,何况还会有遗漏的时候;

经过在网上的搜索:

Ef Core花里胡哨系列(6) XML注释同步到数据库注释-CSDN博客

通过 该作者 提供的代码,做了如下修改:

在项目的 xxx.xxx.EntityFrameworkCore 层下,重写 "xxxDbContext" 类中的 OnModelCreating 方法,代码如下:

cs 复制代码
/// <summary>
/// 重写为模型创建时附带注释内容
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    try
    {
        foreach (var entityType in modelBuilder.Model.GetEntityTypes())
        {
            var typeComment = GetDescription(entityType.ClrType.FullName, "");

            // 存在注释才去生成
            if (!typeComment.IsNullOrWhiteSpace())
                // modelBuilder.Entity(entityType.ClrType).ToTable(t => t.HasComment(typeComment));
                modelBuilder.Entity(entityType.ClrType).HasComment(typeComment);

            foreach (var property in entityType.ClrType.GetProperties().Where(x => x.IsPublic() && x.CanWrite && x.CanRead))
            {
                // 判断是否为值类型,string 除外
                if (!property.PropertyType.IsValueType && typeof(string) != property.PropertyType)
                    continue;

                // (这里是个人对类型的熟悉和练手)
                //if (!property.PropertyType.IsValueType)
                //{

                //    // 对类型进行判断,提出来写美观 ,过滤导航属性 IEntity
                //    if (property.PropertyType.IsGenericType || property.GetSetMethod().IsVirtual)
                //        continue;

                //    // bus_Problem
                //    if (property.PropertyType.GetInterfaces().Any(p => p.IsGenericType && typeof(IEntity<>) == p.GetGenericTypeDefinition()))
                //        continue;
                //}

                var propertyComment = GetDescription(entityType.ClrType.FullName, property.Name);

                // 存在注释才去生成
                if (!propertyComment.IsNullOrEmpty())
                {
                    modelBuilder.Entity(entityType.ClrType).Property(property.Name).HasComment(propertyComment);
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
        throw;
    }

    base.OnModelCreating(modelBuilder);
}

/// <summary>
/// 获取 xml 描述
/// </summary>
/// <param name="xmlFullName">完整的类型名称</param>
/// <param name="columnName">字段名称</param>
/// <returns></returns>
private string GetDescription(string xmlFullName = "", string columnName = "")
{

    // 领域层 xml文件名称地址,记得项目输出 xml 文件哦!!
    string path = Path.Combine(AppContext.BaseDirectory, "xxx.xxx.Core.xml");

    // 加载xml
    XmlDocument xml = new XmlDocument();
    xml.Load(path);

    // 查询节点
    XmlNode classNode = xml.SelectSingleNode($"//member[@name='T:{xmlFullName}']");

    if (classNode == null)
    {
        return string.Empty;
    }

    if (columnName.IsNullOrEmpty())
    {
        return classNode.InnerText.Replace("\n", "").Replace("\r", "").Trim();
    }
    else
    {
        var propertyNode = xml.SelectSingleNode(($"//member[@name='P:{xmlFullName}.{columnName}']"));
        if (propertyNode == null)
        {
            return string.Empty;
        }
       
        return propertyNode.InnerText.Replace("\n", "").Replace("\r", "").Trim();
    }
}

最终运行 命令

cs 复制代码
Add-Migration init

生成出的迁移文件就会包含注释;

这里也在网上找到一个不错的 sql文档生成工具,附带链接一下给大家,好东西免得沉没了。

SmartSQL: 🔥🔥🔥 一款方便、快捷的数据库文档查询、生成工具,支持SqlServer/Oracle/MySql/PostgreSQL/SQLite数据库表结构文档查询、生成;导出文档支持CHM、Word、Excel、PDF、Html、Xml、Json、MarkDown等多种格式。

相关推荐
君莫愁。13 分钟前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎
Lingbug1 小时前
.Net日志组件之NLog的使用和配置
后端·c#·.net·.netcore
咩咩觉主1 小时前
Unity实战案例全解析:PVZ 植物卡片状态分析
unity·c#·游戏引擎
Echo_Lee01 小时前
C#与Python脚本使用共享内存通信
开发语言·python·c#
贾光辉4 小时前
EntityFramework Core并发迁移解决方案
.net core·ef core
__water8 小时前
『功能项目』QFrameWork框架重构OnGUI【63】
c#·unity引擎·重构背包框架
Crazy Struggle8 小时前
C# + WPF 音频播放器 界面优雅,体验良好
c#·wpf·音频播放器·本地播放器
晨曦_子画9 小时前
C#实现指南:将文件夹与exe合并为一个exe
c#
花开莫与流年错_9 小时前
C# 继承父类,base指定构造函数
开发语言·c#·继承·base·构造函数
hillstream310 小时前
oracle中NUMBER(1,0)的字段如何映射到c#中
数据库·oracle·c#