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
{
///
/// 拓展迁移操作:增加数据表和列备注
///
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);
}
///
/// 创建表注释
///
///
///
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();
}
///
/// 创建列注释
///
///
///
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
{
///
/// 命名空间
///
public static string _assemblyNamespace { get; set; } = "Bussiness.Model";
public static List list { get; set; }
///
/// 获取表或者字段属性
///
///
///
///
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;
}
///
/// 获取程序集所有对象注释
///
///
private static List GetDescription()
{
var descriptionList = new List();
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();
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;
}
///
/// 获取属性注释
///
///
private static string GetPropertyDescription(PropertyInfo property)
{
var objs = property.GetCustomAttributes(typeof(DescriptionAttribute), true);
return CommondDescription(objs);
}
///
/// 获取对象注释
///
///
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 "";
}
}
///
/// 表注释信息
///
public class DbDescription
{
///
/// 名称
///
public string Name { get; set; }
///
/// 注释
///
public string Description { get; set; }
///
/// 表字段集合
///
public List Column { get; set; }
}
}