项目 | 版本 | 描述 |
.Net Core | 3.1 | 框架 |
SqlSugarCore | 5.0.1.5 | 数据库实体框架 |
Microscoft Visual Studio 2019 Community | 16.2.0 | 开发环境 |
子属性 | 描述 | 示例 |
{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 |
{using}
namespace {Namespace}
{
{ClassDescription}{SugarTable}
public partial class {ClassName}
{
public {ClassName}(){
{Constructor}
}
{PropertyName}
}
}
SettingClassTemplate 中的 {using} 参数
using System;
using System.Linq;
using System.Text;
SettingClassTemplate 中的 {ClassDescription} 参数
子属性 | 描述 | 示例 |
{ClassDescription} | 类描述 | 学生信息 |
///
///{ClassDescription}
///
SettingClassTemplate 中的 {Constructor} 参数,构造函数没有入参时,此模板无效
this.{PropertyName} ={DefaultValue};
SettingClassTemplate 中的 {PropertyName} 参数
子属性 | 描述 | 示例 |
{PropertyDescription} | 属性描述 | 名称 |
{Default} | 默认值 | NULL::CHARACTER VARYING |
{IsNullable} | 是否为空值 | True |
///
/// Desc:{PropertyDescription}
/// Default:{DefaultValue}
/// Nullable:{IsNullable}
///
示例
///
/// Desc:名称
/// Default:NULL::CHARACTER VARYING
/// Nullable:True
///
SettingClassTemplate 中的 {PropertyName} 参数
子属性 | 描述 | 示例 |
{SugarColumn} | 属性的附加Sugar属性 | [SugarColumn(IsPrimaryKey=true,IsIdentity=true)] |
{PropertyType} | 属性类型 | int |
{PropertyName} | 属性名称 | ID |
{SugarColumn}
public {PropertyType} {PropertyName} {get;set;}
示例
[SugarColumn(IsPrimaryKey=true,IsIdentity=true)]
public int ID {get;set;}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IBLL;
using log4net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.DBEntity;
using Model.MyModel.Config;
using SqlSugar;
using Helper;
namespace web_api_core_1.Controllers
{
[ApiController]
[ApiVersion("1")]
[ApiVersion("2")]
public abstract class ModelDBEntityController :BaseController
{
public ModelDBEntityController(ILog log) : base (log) { }
///
/// 刷新实体模型
///
/// 实体类型文件生成路径
/// 命令空间
[HttpGet]
public string RefreshEntity(string directoryPath = @"C:\Entity", string nameSpace = "Model.DBEntity")
{
try
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = RSSConfig.SqlConnect.ToString(),//连接符字串
DbType = RSSConfig.DbType,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
});
foreach (var item in db.DbMaintenance.GetTableInfoList())
{
string entityName = item.Name.ToUpper();/*实体名大写*/
db.MappingTables.Add(entityName, item.Name);
foreach (var col in db.DbMaintenance.GetColumnInfosByTableName(item.Name))
{
db.MappingColumns.Add(col.DbColumnName.ToUpper() /*类的属性大写*/, col.DbColumnName, entityName);
}
}
db.DbFirst.CreateClassFile(directoryPath, nameSpace);
}
catch(Exception exp)
{
log.Err(exp);
return exp.ToMulString(Helper.StrHelper.HTML.WARP);
}
return "Success";
}
}
}
using System;
using System.Linq;
using System.Text;
using SqlSugar;
namespace Model.DBEntity
{
///
///
///
[SugarTable("machine")]
public partial class MACHINE
{
public MACHINE(){
}
///
/// Desc:机器ID 暂定1:盒剂发药机 2:存取系统
/// Default:'0'::BIGINT
/// Nullable:False
///
[SugarColumn(IsPrimaryKey=true)]
public long MACHINEID {get;set;}
///
/// Desc:机器地址名称
/// Default:NULL::CHARACTER VARYING
/// Nullable:True
///
public string LOCATIONNAME {get;set;}
///
/// Desc:机器IP
/// Default:NULL::CHARACTER VARYING
/// Nullable:True
///
public string MACHINEIP {get;set;}
///
/// Desc:机器状态 1:有效 0:无效
/// Default:1
/// Nullable:True
///
public int? MACHINE_STATE {get;set;}
///
/// Desc:
/// Default:0
/// Nullable:True
///
public int? RUNNING_STATE {get;set;}
///
/// Desc:机器类型 1001:片剂 1002:盒剂 1003:针剂 1004:智能药柜 1005:智能药架 1006:拆零分包机 1009:其它
/// Default:
/// Nullable:True
///
public int? MACHINETYPE {get;set;}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IBLL;
using log4net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.DBEntity;
using Model.MyModel.Config;
using SqlSugar;
using Helper;
namespace web_api_core_1.Controllers
{
[ApiController]
[ApiVersion("1")]
[ApiVersion("2")]
public abstract class ModelDBEntityController :BaseController
{
public ModelDBEntityController(ILog log) : base (log) { }
///
/// 刷新实体模型
///
/// 实体类型文件生成路径
/// 命令空间
[HttpGet]
public string RefreshEntity(string directoryPath = @"C:\Entity", string nameSpace = "Model.DBEntity")
{
try
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = RSSConfig.SqlConnect.ToString(),//连接符字串
DbType = RSSConfig.DbType,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
});
foreach (var item in db.DbMaintenance.GetTableInfoList())
{
string entityName = item.Name.ToUpper();/*实体名大写*/
db.MappingTables.Add(entityName, item.Name);
foreach (var col in db.DbMaintenance.GetColumnInfosByTableName(item.Name))
{
db.MappingColumns.Add(col.DbColumnName.ToUpper() /*类的属性大写*/, col.DbColumnName, entityName);
}
}
db.DbFirst.SettingClassTemplate(o=> { return StrHelper.SugarCustom.ClassTemplate;})
.SettingNamespaceTemplate(o => { return StrHelper.SugarCustom.NamespaceTemplate; })
.SettingClassDescriptionTemplate(o => { return StrHelper.SugarCustom.ClassDescriptionTemplate; })
.SettingConstructorTemplate(o => { return StrHelper.SugarCustom.ConstructorTemplate; })
.SettingPropertyDescriptionTemplate(o => { return StrHelper.SugarCustom.PropertyDescriptionTemplate; })
.SettingPropertyTemplate(o => { return StrHelper.SugarCustom.PropertyTemplate; })
.IsCreateAttribute(true)
.CreateClassFile(directoryPath, nameSpace);
}
catch(Exception exp)
{
log.Err(exp);
return exp.ToMulString(Helper.StrHelper.HTML.WARP);
}
return "Success";
}
}
}
using log4net;
using Microsoft.Extensions.Configuration;
using Model.MyModel;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Helper
{
public class StrHelper
{
///
/// 自定义模板
/// 屏蔽CS1591警告:#pragma warning disable 1591
/// 构造函数添加描述
/// 调整部分模板的空格数量
///
public class SugarCustom
{
///
/// 类模板
///
public const string ClassTemplate = @"{using}
#pragma warning disable 1591
namespace {Namespace}
{
{ClassDescription}{SugarTable}
public partial class {ClassName}
{
///
/// {ClassName}
///
public {ClassName}()
{
{Constructor}
}
{PropertyName}
}
}
";
///
/// 构造函数参数赋值模板
///
public const string ConstructorTemplate = @" this.{PropertyName} = {DefaultValue};
";
///
/// 引用模板
///
public const string NamespaceTemplate = @"using System;
using System.Linq;
using System.Text;
";
///
/// 类描述模板
///
public const string ClassDescriptionTemplate = @" ///
/// {ClassDescription} /// ";
///
/// 属性描述模板
///
public const string PropertyDescriptionTemplate = @" ///
/// 描 述:{PropertyDescription}
/// 默 认 值:{DefaultValue}
/// 是否空值:{IsNullable}
/// ";
///
/// 属性模板
///
public const string PropertyTemplate = @"{SugarColumn}
public {PropertyType} {PropertyName} { get; set; }
";
}
///
/// 默认模板
///
public class SugarDefault
{
///
/// 类模板
///
public const string ClassTemplate = @"{using}
namespace {Namespace}
{
{ClassDescription}{SugarTable}
public partial class {ClassName}
{
public {ClassName}(){
{Constructor}
}
{PropertyName}
}
}
";
///
/// 构造函数参数赋值模板
///
public const string ConstructorTemplate = @" this.{PropertyName} ={DefaultValue};
";
///
/// 引用模板
///
public const string NamespaceTemplate = @"using System;
using System.Linq;
using System.Text;
";
///
/// 类描述模板
///
public const string ClassDescriptionTemplate = @" ///
///{ClassDescription} ///
";
///
/// 属性描述模板
///
public const string PropertyDescriptionTemplate = @" ///
/// Desc:{PropertyDescription}
/// Default:{DefaultValue}
/// Nullable:{IsNullable}
/// ";
///
/// 属性模板
///
public const string PropertyTemplate = @" {SugarColumn}
public {PropertyType} {PropertyName} {get;set;}
";
}
}
}
using System;
using System.Linq;
using System.Text;
using SqlSugar;
#pragma warning disable 1591
namespace Model.DBEntity
{
///
///
///
[SugarTable("machine")]
public partial class MACHINE
{
///
/// MACHINE
///
public MACHINE()
{
}
///
/// 描 述:机器ID 暂定1:盒剂发药机 2:存取系统
/// 默 认 值:0
/// 是否空值:False
///
[SugarColumn(IsPrimaryKey=true,ColumnName="machineID")]
public int MACHINEID { get; set; }
///
/// 描 述:机器类型 1001:片剂 1002:盒剂 1003:针剂 1004:智能药柜 1005:智能药架 1006:拆零分包机 1009:其它
/// 默 认 值:
/// 是否空值:True
///
[SugarColumn(ColumnName="machineType")]
public int? MACHINETYPE { get; set; }
///
/// 描 述:机器地址名称
/// 默 认 值:
/// 是否空值:True
///
[SugarColumn(ColumnName="locationName")]
public string LOCATIONNAME { get; set; }
///
/// 描 述:机器IP
/// 默 认 值:
/// 是否空值:True
///
[SugarColumn(ColumnName="machineIP")]
public string MACHINEIP { get; set; }
///
/// 描 述:机器状态 1:有效 0:无效
/// 默 认 值:1
/// 是否空值:True
///
[SugarColumn(ColumnName="machine_state")]
public byte? MACHINE_STATE { get; set; }
///
/// 描 述:
/// 默 认 值:0
/// 是否空值:True
///
[SugarColumn(ColumnName="running_state")]
public int? RUNNING_STATE { get; set; }
}
}