当前位置: 首页 > news >正文

.net core 通过Sqlsugar生成实体

通过替换字符串的方式生成代码,其他代码也可以通这种方式生成
直接上代码

设置模板

在这里插入图片描述
将这几个模板文件设置为:嵌入资源
在这里插入图片描述
模板内容:

using SqlSugar;namespace {Namespace}.Domain.Admin.{ModelName};
/// <summary>
/// {TableDisplayName}
///</summary>
[SugarTable("{TableName}")]
public class {ModelName}Entity
{{AttributeList}
}

生成代码

 /// <summary>/// 预览代码/// </summary>/// <param name="currentTableName">表名</param>/// <returns></returns>[HttpGet]public ProviewCodeOutput PreviewCode(string currentTableName){//我是在其他类里面生成的代码,所以通过dll加载嵌入的资源// 通过 DLL 加载资源 var assemblyPath = Path.Combine(AppContext.BaseDirectory, "XR.Host.dll");var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);var resourceName = assembly.GetManifestResourceNames().FirstOrDefault(a => a.Contains("ModelTemplate.txt"));var file = assembly.GetManifestResourceStream(resourceName);//实体模板var modelTemplate = new StreamReader(file).ReadToEnd();//仓储接口模板var IrepostoryTemplate = new StreamReader(assembly.GetManifestResourceStream(assembly.GetManifestResourceNames().FirstOrDefault(a => a.Contains("IRepositoryTemplate.txt")))).ReadToEnd();//仓储接口模板var repostoryTemplate = new StreamReader(assembly.GetManifestResourceStream(assembly.GetManifestResourceNames().Where(a => a.Contains("RepositoryClassTemplate.txt")).First())).ReadToEnd();var orm = LazyGetRequiredService<IUserRepository>().Orm;var table = orm.DbMaintenance.GetTableInfoList(true);//命名空间var Namespace = Assembly.GetExecutingAssembly().GetName().Name;var parentPath = new DirectoryInfo(Environment.CurrentDirectory).Parent + $"\\{Namespace}";var result = new ProviewCodeOutput();foreach (var tableInfo in table){if (tableInfo.Name == currentTableName){var modelName = tableInfo.Name.Replace("SYS_", "").Replace("TB_", "").Replace("TN_", "");modelName = ConvertToCamelCase(modelName);var tableColumn = orm.DbMaintenance.GetColumnInfosByTableName(tableInfo.Name);var attributes = BuildColumn(tableColumn);//通过替换字符串的方式生成代码result.ModalCode = modelTemplate.Replace("{Namespace}", Namespace).Replace("{ModelName}", modelName).Replace("{TableName}", tableInfo.Name).Replace("{TableDisplayName}", tableInfo.Description).Replace("{AttributeList}", attributes);}}return result;}private string BuildColumn(List<DbColumnInfo> columnInfos){var attributes = new StringBuilder();foreach (var columnInfo in columnInfos){attributes.Append("\r\n    /// <summary>");attributes.Append($"\r\n   /// {columnInfo.ColumnDescription}");attributes.Append("\r\n    /// </summary>");attributes.Append($"\r\n   [SugarColumn({(columnInfo.IsPrimarykey ? "IsPrimaryKey = true," : "")} ColumnName = \"{columnInfo.DbColumnName}\", {(columnInfo.IsNullable ? "IsNullable = true," : "")} ColumnDescription = \"{columnInfo.ColumnDescription}\")]");attributes.Append($"\r\n   public {SetDataType(columnInfo.DataType)}{(columnInfo.IsNullable ? "?" : "")} {ConvertToCamelCase(columnInfo.DbColumnName)} {{ get; set; }}");}return attributes.ToString();}private string SetDataType(string dataType){dataType = dataType.ToLower();var result = dataType;switch (dataType){case "int32":result = typeof(int).Name;break;case "int64":result = typeof(int).Name;break;case "datetime":result = typeof(DateTime).Name;break;}return result;}/// <summary>/// 将驼峰转换为字符串/// </summary>/// <param name="input"></param>/// <returns></returns>private string ConvertToCamelCase(string input){if (string.IsNullOrEmpty(input))return input;var text = input.Split('_');var camelTxt = "";TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;foreach (var c in text){camelTxt += textInfo.ToTitleCase(c.ToLower());}return camelTxt;}
http://www.lryc.cn/news/437490.html

相关文章:

  • ORCA-3D避障算法解析
  • CentOS 7停更官方yum源无法使用,更换阿里源
  • Introduction结构
  • 基于SpringBoot实现SpringMvc上传下载功能实现
  • vue 控制组件是否显示
  • 生产部门不给力?精益化生产管理咨询公司为您出谋划策
  • HTML+CSS - 网页布局之网格布局
  • 基于51单片机的16X16点阵显示屏proteus仿真
  • 【目标检测数据集】厨房常见的水果蔬菜调味料数据集4910张39类VOC+YOLO格式
  • 在Python中统计字符串中每个字符出现的次数
  • 关于 vue/cli 脚手架实现项目编译运行的源码解析
  • C++笔记---继承(上)
  • 气膜体育馆:为学校打造智能化运动空间—轻空间
  • JVM 调优篇5 jvm性能监控
  • 一. Unity实现虚拟摇杆及屏幕自适应功能
  • 【达梦数据库】mysql 和达梦 tinyint 与 bit 返回值类型差异
  • VUE工程中axios基本使用
  • 跨服务器执行PowerShell脚本
  • linux_L2_linux删除文件
  • 系统架构设计师 - 项目管理
  • Spring Boot基础
  • C语言 | Leetcode C语言题解之第402题移掉K位数字
  • 使用Visual Studio Code配置C/C++开发环境的全面指南
  • 算法练习题26——多项式输出(模拟)
  • 卷积神经网络经典模型架构简介
  • 【Kubernetes】常见面试题汇总(十三)
  • 嵌入式QT开发:构建高效智能的嵌入式系统
  • Linux抢占调度
  • k8s中,为什么把pod的服务以deployment的形式通过nodeport对外发布,以及容器和虚拟机的一些区别
  • PMP--一模--解题--41-50