| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- using Microsoft.EntityFrameworkCore.Metadata;
- using Microsoft.EntityFrameworkCore.Migrations.Operations;
- using Microsoft.EntityFrameworkCore.Migrations;
- using Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal;
- using Pomelo.EntityFrameworkCore.MySql.Migrations;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Reflection;
- using System;
- using System.Linq;
- using Microsoft.EntityFrameworkCore.Storage;
- using Serilog;
- using Microsoft.EntityFrameworkCore.Metadata.Internal;
- namespace Business.EntityFrameworkCore
- {
- /// <summary>
- /// 拓展迁移操作:增加数据表和列备注
- /// </summary>
- public class MyMigrationsSqlGenerator : MySqlMigrationsSqlGenerator
- {
- public MyMigrationsSqlGenerator(
- MigrationsSqlGeneratorDependencies dependencies,
- IRelationalAnnotationProvider migrationsAnnotations,
- IMySqlOptions mySqlOptions)
- : base(dependencies, migrationsAnnotations, mySqlOptions)
- {
- }
- protected override void Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
- {
- base.Generate(operation, model, builder);
- Log.Information("AAAA");
- if (operation is CreateTableOperation || operation is AlterTableOperation)
- CreateTableComment(operation, model, builder);
- if (operation is AddColumnOperation || operation is AlterColumnOperation)
- CreateColumnComment(operation, model, builder);
- }
- /// <summary>
- /// 创建表注释
- /// </summary>
- /// <param name="operation"></param>
- /// <param name="builder"></param>
- private void CreateTableComment(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
- {
- string tableName = string.Empty;
- string description = string.Empty;
- if (operation is AlterTableOperation)
- {
- var t = operation as AlterColumnOperation;
- tableName = (operation as AlterTableOperation).Name;
- }
- if (operation is CreateTableOperation)
- {
- var t = operation as CreateTableOperation;
- var addColumnsOperation = t.Columns;
- tableName = t.Name;
- foreach (var item in addColumnsOperation)
- {
- CreateColumnComment(item, model, builder);
- }
- }
- description = DbDescriptionHelper.GetDescription(tableName);
- Log.Debug($"创建BOM表,返回备注{description}");
- if (tableName.IsNullOrWhiteSpace())
- throw new Exception("表名为空引起添加表注释异常.");
- var sqlHelper = Dependencies.SqlGenerationHelper;
- builder
- .Append("ALTER TABLE ")
- .Append(sqlHelper.DelimitIdentifier(tableName))
- .Append(" COMMENT ")
- .Append("'")
- .Append(description)
- .Append("'")
- .AppendLine(sqlHelper.StatementTerminator)
- .EndCommand();
- }
- /// <summary>
- /// 创建列注释
- /// </summary>
- /// <param name="operation"></param>
- /// <param name="builder"></param>
- private void CreateColumnComment(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
- {
- //alter table a1log modify column UUID VARCHAR(26) comment '修改后的字段注释';
- string tableName = string.Empty;
- string columnName = string.Empty;
- string columnType = string.Empty;
- string description = string.Empty;
- if (operation is AlterColumnOperation)
- {
- var t = (operation as AlterColumnOperation);
- tableName = t.Table;
- columnName = t.Name;
- var o = new AlterColumnOperation();
- o.ClrType = t.ClrType;
- o.ColumnType = t.ColumnType;
- o.Comment = t.Comment;
- o.IsUnicode = t.IsUnicode;
- o.MaxLength = t.MaxLength;
- o.IsFixedLength = t.IsFixedLength;
- o.IsRowVersion = t.IsRowVersion;
- columnType = base.GetColumnType(t.Schema, t.Table, t.Name, o, model);
- }
- if (operation is AddColumnOperation)
- {
- var t = (operation as AddColumnOperation);
- tableName = t.Table;
- columnName = t.Name;
- var o = new AddColumnOperation();
- o.ClrType = t.ClrType;
- o.ColumnType = t.ColumnType;
- o.Comment = t.Comment;
- o.IsUnicode = t.IsUnicode;
- o.MaxLength = t.MaxLength;
- o.IsFixedLength = t.IsFixedLength;
- o.IsRowVersion = t.IsRowVersion;
- columnType = GetColumnType(t.Schema, t.Table, t.Name, o, model);
- description = DbDescriptionHelper.GetDescription(tableName, columnName);
- }
- if (columnName.IsNullOrWhiteSpace() || tableName.IsNullOrWhiteSpace() || columnType.IsNullOrWhiteSpace())
- throw new Exception("列名为空或表名为空或列类型为空引起添加列注释异常." + columnName + "/" + tableName + "/" + columnType);
- var sqlHelper = Dependencies.SqlGenerationHelper;
- builder
- .Append("ALTER TABLE ")
- .Append(sqlHelper.DelimitIdentifier(tableName))
- .Append(" MODIFY COLUMN ")
- .Append($"`{columnName}`")
- .Append(" ")
- .Append(columnType)
- .Append(" COMMENT ")
- .Append("'")
- .Append(description)
- .Append("'")
- .AppendLine(sqlHelper.StatementTerminator)
- .EndCommand();
- }
- }
- public class DbDescriptionHelper
- {
- /// <summary>
- /// 命名空间
- /// </summary>
- public static string _assemblyNamespace { get; set; } = "Bussiness.Model";
- public static List<DbDescription> list { get; set; }
- /// <summary>
- /// 获取表或者字段属性
- /// </summary>
- /// <param name="table"></param>
- /// <param name="column"></param>
- /// <returns></returns>
- public static string GetDescription(string table, string column = "")
- {
- Log.Debug($"AAAA:{table}-----{column}");
- if (list == null || list.Count== 0)
- {
- list = GetDescription();
- }
- if (!string.IsNullOrWhiteSpace(table))
- {
- if (string.IsNullOrWhiteSpace(column))
- {
- var x = list.FirstOrDefault(p => p.Name == table);
- if (x != null)
- return x.Description;
- return string.Empty;
- }
- else
- {
- var x = list.FirstOrDefault(p => p.Name == table);
- if (x != null)
- {
- var y = x.Column;
- if (y != null)
- {
- var z = y.FirstOrDefault(p => p.Name == column);
- if (z != null)
- return z.Description;
- }
- }
- return string.Empty;
- }
- }
- else
- return string.Empty;
- }
- /// <summary>
- /// 获取程序集所有对象注释
- /// </summary>
- /// <returns></returns>
- private static List<DbDescription> GetDescription()
- {
- var descriptionList = new List<DbDescription>();
- var entityAssembly = Assembly.Load(_assemblyNamespace);
- var allType = entityAssembly?.GetTypes();
- var allClass = allType?.Where(t => t.IsClass).ToList();
- foreach (var h in allClass)
- {
- //表注释
- var tableDescription = new DbDescription();
- tableDescription.Column = new List<DbDescription>();
- tableDescription.Name = h.Name;
- var tobjs = h.GetCustomAttributes(typeof(DescriptionAttribute), true);
- tableDescription.Description = GetPropertyDescription(h);
- //所有属性注释
- PropertyInfo[] peroperties = h.GetProperties();
- foreach (PropertyInfo property in peroperties)
- {
- var column = new DbDescription();
- column.Name = property.Name;
- column.Description = GetPropertyDescription(property);
- tableDescription.Column.Add(column);
- }
- descriptionList.Add(tableDescription);
- }
- return descriptionList;
- }
- /// <summary>
- /// 获取属性注释
- /// </summary>
- /// <returns></returns>
- private static string GetPropertyDescription(PropertyInfo property)
- {
- var objs = property.GetCustomAttributes(typeof(DescriptionAttribute), true);
- return CommondDescription(objs);
- }
- /// <summary>
- /// 获取对象注释
- /// </summary>
- /// <returns></returns>
- private static string GetPropertyDescription(Type type)
- {
- var objs = type.GetCustomAttributes(typeof(DescriptionAttribute), true);
- return CommondDescription(objs);
- }
- private static string CommondDescription(object[] objs)
- {
- if (objs != null && objs.Length > 0)
- {
- return ((DescriptionAttribute)objs[0]).Description;
- }
- return "";
- }
- }
- /// <summary>
- /// 表注释信息
- /// </summary>
- public class DbDescription
- {
- /// <summary>
- /// 名称
- /// </summary>
- public string Name { get; set; }
- /// <summary>
- /// 注释
- /// </summary>
- public string Description { get; set; }
- /// <summary>
- /// 表字段集合
- /// </summary>
- public List<DbDescription> Column { get; set; }
- }
- }
|