当前位置:   article > 正文

【ASP .NET CORE 】SqlSugar DbFirst生成实体框架

sqlsugar dbfirst

一、基本信息

项目版本描述
.Net Core3.1框架
SqlSugarCore5.0.1.5数据库实体框架
Microscoft Visual Studio 2019 Community16.2.0开发环境

 

 

二、默认模板

2.1、类模板 SettingClassTemplate

子属性描述示例
{using}引用见 2.2
{Namespace}命名空间,CreateClassFile函数的第二参数Model.Entity
{ClassDescription}类描述见 2.3
{SugarTable}类的Sugar属性[SugarTable("CHECK_PARAM")]
{ClassName}类名 = 表/视图名称Student
{Constructor}构造函数中的参数赋值见 2.4
{PropertyName}

属性描述&名称

见 2.5 & 2.6
  1. {using}
  2. namespace {Namespace}
  3. {
  4. {ClassDescription}{SugarTable}
  5. public partial class {ClassName}
  6. {
  7. public {ClassName}(){
  8. {Constructor}
  9. }
  10. {PropertyName}
  11. }
  12. }

 

2.2、引用模板 SettingNamespaceTemplate

SettingClassTemplate 中的 {using} 参数

  1. using System;
  2. using System.Linq;
  3. using System.Text;

2.3、类描述模板 SettingClassDescriptionTemplate

SettingClassTemplate 中的 {ClassDescription} 参数

子属性描述示例
{ClassDescription}类描述学生信息
  1. ///<summary>
  2. ///{ClassDescription}
  3. ///</summary>

2.4、构造参数赋值模板 SettingConstructorTemplate

SettingClassTemplate 中的 {Constructor} 参数,构造函数没有入参时,此模板无效

  1. this.{PropertyName} ={DefaultValue};

2.5、属性描述模板 SettingPropertyDescriptionTemplate

SettingClassTemplate 中的 {PropertyName} 参数

子属性描述示例
{PropertyDescription}属性描述名称
{Default}默认值NULL::CHARACTER VARYING
{IsNullable}是否为空值True
  1. /// <summary>
  2. /// Desc:{PropertyDescription}
  3. /// Default:{DefaultValue}
  4. /// Nullable:{IsNullable}
  5. /// </summary>

示例 

  1. /// <summary>
  2. /// Desc:名称
  3. /// Default:NULL::CHARACTER VARYING
  4. /// Nullable:True
  5. /// </summary>

 

2.6、属性模板 SettingPropertyTemplate

SettingClassTemplate 中的 {PropertyName} 参数

子属性描述示例
{SugarColumn}属性的附加Sugar属性

[SugarColumn(IsPrimaryKey=true,IsIdentity=true)]

{PropertyType}属性类型int
{PropertyName}属性名称ID
  1. {SugarColumn}
  2. public {PropertyType} {PropertyName} {get;set;}

示例

  1. [SugarColumn(IsPrimaryKey=true,IsIdentity=true)]
  2. public int ID {get;set;}

三、示例

3.1、创建API,使用默认模板生成实体

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using IBLL;
  6. using log4net;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Model.DBEntity;
  11. using Model.MyModel.Config;
  12. using SqlSugar;
  13. using Helper;
  14. namespace web_api_core_1.Controllers
  15. {
  16. [ApiController]
  17. [ApiVersion("1")]
  18. [ApiVersion("2")]
  19. public abstract class ModelDBEntityController :BaseController
  20. {
  21. public ModelDBEntityController(ILog log) : base (log) { }
  22. /// <summary>
  23. /// 刷新实体模型
  24. /// </summary>
  25. /// <param name="directoryPath">实体类型文件生成路径</param>
  26. /// <param name="nameSpace">命令空间</param>
  27. [HttpGet]
  28. public string RefreshEntity(string directoryPath = @"C:\Entity", string nameSpace = "Model.DBEntity")
  29. {
  30. try
  31. {
  32. SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
  33. {
  34. ConnectionString = RSSConfig.SqlConnect.ToString(),//连接符字串
  35. DbType = RSSConfig.DbType,
  36. IsAutoCloseConnection = true,
  37. InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
  38. });
  39. foreach (var item in db.DbMaintenance.GetTableInfoList())
  40. {
  41. string entityName = item.Name.ToUpper();/*实体名大写*/
  42. db.MappingTables.Add(entityName, item.Name);
  43. foreach (var col in db.DbMaintenance.GetColumnInfosByTableName(item.Name))
  44. {
  45. db.MappingColumns.Add(col.DbColumnName.ToUpper() /*类的属性大写*/, col.DbColumnName, entityName);
  46. }
  47. }
  48. db.DbFirst.CreateClassFile(directoryPath, nameSpace);
  49. }
  50. catch(Exception exp)
  51. {
  52. log.Err(exp);
  53. return exp.ToMulString(Helper.StrHelper.HTML.WARP);
  54. }
  55. return "Success";
  56. }
  57. }
  58. }

3.1.2、生成效果

  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using SqlSugar;
  5. namespace Model.DBEntity
  6. {
  7. ///<summary>
  8. ///
  9. ///</summary>
  10. [SugarTable("machine")]
  11. public partial class MACHINE
  12. {
  13. public MACHINE(){
  14. }
  15. /// <summary>
  16. /// Desc:机器ID 暂定1:盒剂发药机 2:存取系统
  17. /// Default:'0'::BIGINT
  18. /// Nullable:False
  19. /// </summary>
  20. [SugarColumn(IsPrimaryKey=true)]
  21. public long MACHINEID {get;set;}
  22. /// <summary>
  23. /// Desc:机器地址名称
  24. /// Default:NULL::CHARACTER VARYING
  25. /// Nullable:True
  26. /// </summary>
  27. public string LOCATIONNAME {get;set;}
  28. /// <summary>
  29. /// Desc:机器IP
  30. /// Default:NULL::CHARACTER VARYING
  31. /// Nullable:True
  32. /// </summary>
  33. public string MACHINEIP {get;set;}
  34. /// <summary>
  35. /// Desc:机器状态 1:有效 0:无效
  36. /// Default:1
  37. /// Nullable:True
  38. /// </summary>
  39. public int? MACHINE_STATE {get;set;}
  40. /// <summary>
  41. /// Desc:
  42. /// Default:0
  43. /// Nullable:True
  44. /// </summary>
  45. public int? RUNNING_STATE {get;set;}
  46. /// <summary>
  47. /// Desc:机器类型 1001:片剂 1002:盒剂 1003:针剂 1004:智能药柜 1005:智能药架 1006:拆零分包机 1009:其它
  48. /// Default:
  49. /// Nullable:True
  50. /// </summary>
  51. public int? MACHINETYPE {get;set;}
  52. }
  53. }

 

 3.2、给构造函数添加注释,并屏蔽警告 CS1591

3.2.1、由数据库生成实体模型

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using IBLL;
  6. using log4net;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Model.DBEntity;
  11. using Model.MyModel.Config;
  12. using SqlSugar;
  13. using Helper;
  14. namespace web_api_core_1.Controllers
  15. {
  16. [ApiController]
  17. [ApiVersion("1")]
  18. [ApiVersion("2")]
  19. public abstract class ModelDBEntityController :BaseController
  20. {
  21. public ModelDBEntityController(ILog log) : base (log) { }
  22. /// <summary>
  23. /// 刷新实体模型
  24. /// </summary>
  25. /// <param name="directoryPath">实体类型文件生成路径</param>
  26. /// <param name="nameSpace">命令空间</param>
  27. [HttpGet]
  28. public string RefreshEntity(string directoryPath = @"C:\Entity", string nameSpace = "Model.DBEntity")
  29. {
  30. try
  31. {
  32. SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
  33. {
  34. ConnectionString = RSSConfig.SqlConnect.ToString(),//连接符字串
  35. DbType = RSSConfig.DbType,
  36. IsAutoCloseConnection = true,
  37. InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
  38. });
  39. foreach (var item in db.DbMaintenance.GetTableInfoList())
  40. {
  41. string entityName = item.Name.ToUpper();/*实体名大写*/
  42. db.MappingTables.Add(entityName, item.Name);
  43. foreach (var col in db.DbMaintenance.GetColumnInfosByTableName(item.Name))
  44. {
  45. db.MappingColumns.Add(col.DbColumnName.ToUpper() /*类的属性大写*/, col.DbColumnName, entityName);
  46. }
  47. }
  48. db.DbFirst.SettingClassTemplate(o=> { return StrHelper.SugarCustom.ClassTemplate;})
  49. .SettingNamespaceTemplate(o => { return StrHelper.SugarCustom.NamespaceTemplate; })
  50. .SettingClassDescriptionTemplate(o => { return StrHelper.SugarCustom.ClassDescriptionTemplate; })
  51. .SettingConstructorTemplate(o => { return StrHelper.SugarCustom.ConstructorTemplate; })
  52. .SettingPropertyDescriptionTemplate(o => { return StrHelper.SugarCustom.PropertyDescriptionTemplate; })
  53. .SettingPropertyTemplate(o => { return StrHelper.SugarCustom.PropertyTemplate; })
  54. .IsCreateAttribute(true)
  55. .CreateClassFile(directoryPath, nameSpace);
  56. }
  57. catch(Exception exp)
  58. {
  59. log.Err(exp);
  60. return exp.ToMulString(Helper.StrHelper.HTML.WARP);
  61. }
  62. return "Success";
  63. }
  64. }
  65. }

3.2.2、StrHelper.cs > StrHelper.SugarCustom

  1. using log4net;
  2. using Microsoft.Extensions.Configuration;
  3. using Model.MyModel;
  4. using SqlSugar;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Text;
  9. namespace Helper
  10. {
  11. public class StrHelper
  12. {
  13. /// <summary>
  14. /// 自定义模板
  15. /// <para>屏蔽CS1591警告:#pragma warning disable 1591</para>
  16. /// <para>构造函数添加描述</para>
  17. /// <para>调整部分模板的空格数量</para>
  18. /// </summary>
  19. public class SugarCustom
  20. {
  21. /// <summary>
  22. /// 类模板
  23. /// </summary>
  24. public const string ClassTemplate = @"{using}
  25. #pragma warning disable 1591
  26. namespace {Namespace}
  27. {
  28. {ClassDescription}{SugarTable}
  29. public partial class {ClassName}
  30. {
  31. /// <summary>
  32. /// {ClassName}
  33. /// </summary>
  34. public {ClassName}()
  35. {
  36. {Constructor}
  37. }
  38. {PropertyName}
  39. }
  40. }
  41. ";
  42. /// <summary>
  43. /// 构造函数参数赋值模板
  44. /// </summary>
  45. public const string ConstructorTemplate = @" this.{PropertyName} = {DefaultValue};
  46. ";
  47. /// <summary>
  48. /// 引用模板
  49. /// </summary>
  50. public const string NamespaceTemplate = @"using System;
  51. using System.Linq;
  52. using System.Text;
  53. ";
  54. /// <summary>
  55. /// 类描述模板
  56. /// </summary>
  57. public const string ClassDescriptionTemplate = @" /// <summary>
  58. /// {ClassDescription} /// </summary>";
  59. /// <summary>
  60. /// 属性描述模板
  61. /// </summary>
  62. public const string PropertyDescriptionTemplate = @" /// <summary>
  63. /// 描 述:{PropertyDescription}
  64. /// 默 认 值:{DefaultValue}
  65. /// 是否空值:{IsNullable}
  66. /// </summary>";
  67. /// <summary>
  68. /// 属性模板
  69. /// </summary>
  70. public const string PropertyTemplate = @"{SugarColumn}
  71. public {PropertyType} {PropertyName} { get; set; }
  72. ";
  73. }
  74. /// <summary>
  75. /// 默认模板
  76. /// </summary>
  77. public class SugarDefault
  78. {
  79. /// <summary>
  80. /// 类模板
  81. /// </summary>
  82. public const string ClassTemplate = @"{using}
  83. namespace {Namespace}
  84. {
  85. {ClassDescription}{SugarTable}
  86. public partial class {ClassName}
  87. {
  88. public {ClassName}(){
  89. {Constructor}
  90. }
  91. {PropertyName}
  92. }
  93. }
  94. ";
  95. /// <summary>
  96. /// 构造函数参数赋值模板
  97. /// </summary>
  98. public const string ConstructorTemplate = @" this.{PropertyName} ={DefaultValue};
  99. ";
  100. /// <summary>
  101. /// 引用模板
  102. /// </summary>
  103. public const string NamespaceTemplate = @"using System;
  104. using System.Linq;
  105. using System.Text;
  106. ";
  107. /// <summary>
  108. /// 类描述模板
  109. /// </summary>
  110. public const string ClassDescriptionTemplate = @" ///<summary>
  111. ///{ClassDescription} ///</summary>
  112. ";
  113. /// <summary>
  114. /// 属性描述模板
  115. /// </summary>
  116. public const string PropertyDescriptionTemplate = @" /// <summary>
  117. /// Desc:{PropertyDescription}
  118. /// Default:{DefaultValue}
  119. /// Nullable:{IsNullable}
  120. /// </summary>";
  121. /// <summary>
  122. /// 属性模板
  123. /// </summary>
  124. public const string PropertyTemplate = @" {SugarColumn}
  125. public {PropertyType} {PropertyName} {get;set;}
  126. ";
  127. }
  128. }
  129. }

3.2.3、生成效果 

  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using SqlSugar;
  5. #pragma warning disable 1591
  6. namespace Model.DBEntity
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. [SugarTable("machine")]
  12. public partial class MACHINE
  13. {
  14. /// <summary>
  15. /// MACHINE
  16. /// </summary>
  17. public MACHINE()
  18. {
  19. }
  20. /// <summary>
  21. /// 描 述:机器ID 暂定1:盒剂发药机 2:存取系统
  22. /// 默 认 值:0
  23. /// 是否空值:False
  24. /// </summary>
  25. [SugarColumn(IsPrimaryKey=true,ColumnName="machineID")]
  26. public int MACHINEID { get; set; }
  27. /// <summary>
  28. /// 描 述:机器类型 1001:片剂 1002:盒剂 1003:针剂 1004:智能药柜 1005:智能药架 1006:拆零分包机 1009:其它
  29. /// 默 认 值:
  30. /// 是否空值:True
  31. /// </summary>
  32. [SugarColumn(ColumnName="machineType")]
  33. public int? MACHINETYPE { get; set; }
  34. /// <summary>
  35. /// 描 述:机器地址名称
  36. /// 默 认 值:
  37. /// 是否空值:True
  38. /// </summary>
  39. [SugarColumn(ColumnName="locationName")]
  40. public string LOCATIONNAME { get; set; }
  41. /// <summary>
  42. /// 描 述:机器IP
  43. /// 默 认 值:
  44. /// 是否空值:True
  45. /// </summary>
  46. [SugarColumn(ColumnName="machineIP")]
  47. public string MACHINEIP { get; set; }
  48. /// <summary>
  49. /// 描 述:机器状态 1:有效 0:无效
  50. /// 默 认 值:1
  51. /// 是否空值:True
  52. /// </summary>
  53. [SugarColumn(ColumnName="machine_state")]
  54. public byte? MACHINE_STATE { get; set; }
  55. /// <summary>
  56. /// 描 述:
  57. /// 默 认 值:0
  58. /// 是否空值:True
  59. /// </summary>
  60. [SugarColumn(ColumnName="running_state")]
  61. public int? RUNNING_STATE { get; set; }
  62. }
  63. }

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号