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); } } }