using SqlSugar; using System; using System.Collections.Generic; using System.Reflection; using System.Text; namespace DDD { /// <summary> /// /// SqlSugarClient 代码优先建表 /// 根据给定的实体类,创建SQL语句, 之后创建MySQL表 /// /// 优势: /// 随便给一个C# 实体类,就可以建表。 /// 无需给实体类的属性添加额外的属性。如:繁琐的 [SugarColumn(IsPrimaryKey =true)]等。 /// /// 用法: /// CreateMySQLTableCodeFirst.CreateTablesBySQL(DB, typeof(YourType1),typeof(YourType2)); /// /// 后续做法: /// 如果对字段的类型不满意,可以用 HeidiSQL 软件修改表的字段和类型 /// 添加字段、修改字段名称、添加索引,也用该软件。 /// /// by lmp 2024.11.15 /// /// </summary> public class CreateTableCodeFirstOnMySQL { /// <summary> /// 创建表 /// </summary> /// <param name="db"></param> /// <param name="entityTypes"></param> public static void CreateTablesBySQL(SqlSugarClient db, params Type[] entityTypes) { foreach (Type entityType in entityTypes) { string sql = GetCreateTableSQLOfType(entityType); db.Ado.ExecuteCommand(sql); Log.Msg($"创建表完成 【{entityType.Name}】"); } } /// <summary> /// 生成建表SQL语句 /// </summary> /// <param name="entityType"></param> /// <returns></returns> public static string GetCreateTableSQLOfType(Type entityType) { string tableName = entityType.Name; string sqlPropertys = CreateOther(entityType); StringBuilder sb = new StringBuilder(); sb.Append($@" CREATE TABLE `{tableName}` ( `Id` BIGINT NOT NULL AUTO_INCREMENT, {sqlPropertys}, PRIMARY KEY (`Id`) USING BTREE ) COLLATE='utf8mb4_0900_ai_ci' ENGINE=InnoDB "); return sb.ToString(); } /// <summary> /// 创建除了Id 以外的其他类型 /// </summary> /// <param name="entityType"></param> /// <returns></returns> /// <exception cref="Exception"></exception> private static string CreateOther(Type entityType) { PropertyInfo[] piArray = entityType.GetProperties(); // 类型T的所有公共属性 List<string> items = new List<string>(); foreach (PropertyInfo pi in piArray) { string columnName = pi.Name; if (columnName.ToUpper() == "ID") // 排除Id ,因为已经创建了语句 { continue; } Type tp = pi.PropertyType; if (tp == typeof(DateTime) || tp == typeof(DateTime?)) { items.Add($"`{columnName}` DATETIME NULL "); } else if (tp == typeof(float) || tp == typeof(float?)) { items.Add($"`{columnName}` DOUBLE NULL DEFAULT 0 "); } else if (tp == typeof(double) || tp == typeof(double?)) { items.Add($"`{columnName}` DOUBLE NULL DEFAULT 0 "); } else if (tp == typeof(decimal) || tp == typeof(decimal?)) { items.Add($"`{columnName}` DOUBLE NULL DEFAULT 0 "); } else if (tp == typeof(int) || tp == typeof(int?)) { items.Add($"`{columnName}` INT NULL DEFAULT 0 "); } else if (tp == typeof(long) || tp == typeof(long?)) { items.Add($"`{columnName}` BIGINT NULL DEFAULT 0 "); } else if (tp == typeof(bool) || tp == typeof(bool?)) { items.Add($"`{columnName}` TINYINT(1) NULL DEFAULT 0 "); } else if (tp == typeof(string)) { items.Add($"`{columnName}` VARCHAR(255) NULL DEFAULT NULL "); } else if (tp == typeof(byte[])) { items.Add($"`{columnName}` LONGBLOB NULL DEFAULT NULL "); } else { throw new Exception($"{entityType.Name} 中{columnName}的未知类型:" + tp); } } return string.Join(", \r\n", items); } } }
标签:实体类,建表,创建,entityType,tp,columnName,typeof,items,NULL From: https://www.cnblogs.com/lmp138/p/18548830