1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | --DictParam,表名称 declare @TableName sysname = 'DictParam' declare @Result varchar ( max ) = ' /// <summary> /// ' + @TableName + ' /// </summary> public class ' + @TableName + ' {' select @Result = @Result + ' /// <summary> /// ' + CONVERT (NVARCHAR(500), ISNULL (ColName, '' )) + ' /// </summary> public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; } ' from ( SELECT replace (col. name , ' ' , '_' ) ColumnName, column_id ColumnId, prop.value ColName, case typ. name when 'bigint' then 'long' when 'binary' then 'byte[]' when 'bit' then 'bool' when 'char' then 'string' when 'date' then 'DateTime' when 'datetime' then 'DateTime' when 'datetime2' then 'DateTime' when 'datetimeoffset' then 'DateTimeOffset' when 'decimal' then 'decimal' when 'float' then 'float' when 'image' then 'byte[]' when 'int' then 'int' when 'money' then 'decimal' when 'nchar' then 'char' when 'ntext' then 'string' when 'numeric' then 'decimal' when 'nvarchar' then 'string' when 'real' then 'double' when 'smalldatetime' then 'DateTime' when 'smallint' then 'short' when 'smallmoney' then 'decimal' when 'text' then 'string' when 'time' then 'TimeSpan' when 'timestamp' then 'DateTime' when 'tinyint' then 'byte' when 'uniqueidentifier' then 'Guid' when 'varbinary' then 'byte[]' when 'varchar' then 'string' else 'UNKNOWN_' + typ. name end ColumnType, case when col.is_nullable = 1 and typ. name in ( 'bigint' , 'bit' , 'date' , 'datetime' , 'datetime2' , 'datetimeoffset' , 'decimal' , 'float' , 'int' , 'money' , 'numeric' , 'real' , 'smalldatetime' , 'smallint' , 'smallmoney' , 'time' , 'tinyint' , 'uniqueidentifier' ) then '?' else '' end NullableSign from sys.columns col join sys.types typ on col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id LEFT JOIN sys.extended_properties prop ON col.object_id = prop.major_id AND col.column_id = prop.minor_id where object_id = object_id(@TableName) ) t --order by ColumnId set @Result = @Result + ' }' print @Result |
注:用于生成实体类对象。如果写了字段说明自带属性注释
不生成注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | declare @TableName sysname = 'DictPublic' declare @Result varchar(max) = ' /// <summary> /// ' + @TableName + ' /// </summary> public class ' + @TableName + ' {' select @Result = @Result + ' public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get ; set ; }' from ( SELECT replace(col.name, ' ' , '_' ) ColumnName, column_id ColumnId, prop.value ColName, case typ.name when 'bigint' then 'long' when 'binary' then 'byte[]' when 'bit' then 'bool' when 'char' then 'string' when 'date' then 'DateTime' when 'datetime' then 'DateTime' when 'datetime2' then 'DateTime' when 'datetimeoffset' then 'DateTimeOffset' when 'decimal' then 'decimal' when 'float' then 'float' when 'image' then 'byte[]' when 'int' then 'int' when 'money' then 'decimal' when 'nchar' then 'char' when 'ntext' then 'string' when 'numeric' then 'decimal' when 'nvarchar' then 'string' when 'real' then 'double' when 'smalldatetime' then 'DateTime' when 'smallint' then 'short' when 'smallmoney' then 'decimal' when 'text' then 'string' when 'time' then 'TimeSpan' when 'timestamp' then 'DateTime' when 'tinyint' then 'byte' when 'uniqueidentifier' then 'Guid' when 'varbinary' then 'byte[]' when 'varchar' then 'string' else 'UNKNOWN_' + typ.name end ColumnType, case when col.is_nullable = 1 and typ.name in ( 'bigint' , 'bit' , 'date' , 'datetime' , 'datetime2' , 'datetimeoffset' , 'decimal' , 'float' , 'int' , 'money' , 'numeric' , 'real' , 'smalldatetime' , 'smallint' , 'smallmoney' , 'time' , 'tinyint' , 'uniqueidentifier' ) then '?' else '' end NullableSign from sys.columns col join sys.types typ on col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id LEFT JOIN sys.extended_properties prop ON col.object_id = prop.major_id AND col.column_id = prop.minor_id where object_id = object_id(@TableName) ) t --order by ColumnId set @Result = @Result + ' }' print @Result |
根据实际工作更改如下(生成部分代码):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | -------------------------------生成实体类------------------------------- declare @TableName sysname = 'DictPublic' --declare @TableNameL sysname = 'dictPublic' declare @TableNameL varchar (200) set @TableNameL = ( select ( lower ( left (@TableName,1))+ SUBSTRING (@TableName,2,len(@TableName)))) --print @TableNameL declare @Result varchar ( max ) = ' /// <summary> /// ' + @TableName + ' /// </summary> public class ' + @TableName + ' {' select @Result = @Result + ' public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }' from ( SELECT replace (col. name , ' ' , '_' ) ColumnName, column_id ColumnId, prop.value ColName, case typ. name when 'bigint' then 'long' when 'binary' then 'byte[]' when 'bit' then 'bool' when 'char' then 'string' when 'date' then 'DateTime' when 'datetime' then 'DateTime' when 'datetime2' then 'DateTime' when 'datetimeoffset' then 'DateTimeOffset' when 'decimal' then 'decimal' when 'float' then 'float' when 'image' then 'byte[]' when 'int' then 'int' when 'money' then 'decimal' when 'nchar' then 'char' when 'ntext' then 'string' when 'numeric' then 'decimal' when 'nvarchar' then 'string' when 'real' then 'double' when 'smalldatetime' then 'DateTime' when 'smallint' then 'short' when 'smallmoney' then 'decimal' when 'text' then 'string' when 'time' then 'TimeSpan' when 'timestamp' then 'DateTime' when 'tinyint' then 'byte' when 'uniqueidentifier' then 'Guid' when 'varbinary' then 'byte[]' when 'varchar' then 'string' else 'UNKNOWN_' + typ. name end ColumnType, case when col.is_nullable = 1 and typ. name in ( 'bigint' , 'bit' , 'date' , 'datetime' , 'datetime2' , 'datetimeoffset' , 'decimal' , 'float' , 'int' , 'money' , 'numeric' , 'real' , 'smalldatetime' , 'smallint' , 'smallmoney' , 'time' , 'tinyint' , 'uniqueidentifier' ) then '?' else '' end NullableSign from sys.columns col join sys.types typ on col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id LEFT JOIN sys.extended_properties prop ON col.object_id = prop.major_id AND col.column_id = prop.minor_id where object_id = object_id(@TableName) ) t --order by ColumnId set @Result = @Result + ' }' print @Result -------------------------------生成实体类------------------------------- print '' --IDAL --public partial interface IDictQualityItemsDal : IBaseDal<DictQualityItems> print 'public partial interface I' +@TableName + 'Dal : IBaseDal<' + @TableName + '>{}' print '' --bool Inserts(List<DictQualityItems> objs); --bool DeleteByIds(List<string> ids); --bool UpdateIsEnable(List<string> ids, string isEnable); --DAL --public partial class DictQualityItemsDal : BaseDal<DictQualityItems>, IDictQualityItemsDal print 'public partial class ' +@TableName + 'Dal : BaseDal<' + @TableName + '>,I' +@TableName+ 'Dal{}' print '' --IService --public partial interface IDictReviewItemsService : IBaseService<DictReviewItems> print 'public partial interface I' +@TableName + 'Service : IBaseService<' + @TableName + '>{}' print '' --Service --public partial class DictQualityItemsService : BaseService<DictQualityItems>, IDictQualityItemsService --private IDictQualityItemsDal Dal = new DictQualityItemsDal(); --public override void SetCurrentDal() --CurrentDal = new DictQualityItemsDal(); print 'public partial class ' +@TableName + 'Service : BaseService<' + @TableName + '>,I' +@TableName+ 'Service' print '{' print ' private I' + @TableName + 'Dal Dal = new ' +@TableName + 'Dal();' print ' public override void SetCurrentDal()' print ' {' print ' CurrentDal = new ' + @TableName + 'Dal();' print ' }' print '' print '}' --Controller declare @Controller varchar ( max ) = ' /// <summary> /// ' + @TableName + ' 控制器 /// </summary>' + char (13) + '[ApiController]' + char (13) + 'public class ' + @TableName + 'Controller : ControllerBase' + char (13) + '{' + char (13) + ' #region 定义' + char (13) + ' /// <summary> /// I' + @TableName + 'Service ' + @TableName + 'Service /// </summary>' + char (13) + ' private I' + @TableName + 'Service ' + @TableName + 'Service; ' + ' /// <summary> /// 初始化服务信息 /// </summary>' + char (13) + ' /// <param name="' +@TableNameL+ 'Service">I' + @TableName + 'Service ' + @TableNameL + 'Service</param>' + char (13) + ' public ' + @TableName + 'Controller(I' + @TableName+ 'Service ' + @TableNameL + 'Service)' + char (13) + ' {' + char (13) + ' ' +@TableName+ 'Service = ' + @TableNameL+ 'Service;' + char (13) + ' }' + char (13) + char (13)+ ' #endregion' + char (13) + char (13) + '}' + char (13) print @Controller |
生成代码如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | /// <summary> /// DictPublic /// </summary> public class DictPublic { public string PublicNo { get ; set ; } public string DictLevel { get ; set ; } public string DictName { get ; set ; } public string DictValue { get ; set ; } public string OperateType { get ; set ; } public string DictParentId { get ; set ; } public decimal OrderNo { get ; set ; } public string UseDept { get ; set ; } public string Remark { get ; set ; } public decimal IsEnable { get ; set ; } public decimal IsDelete { get ; set ; } public string DeleteUser { get ; set ; } public string DeleteTime { get ; set ; } } public partial interface IDictPublicDal : IBaseDal<DictPublic>{} public partial class DictPublicDal : BaseDal<DictPublic>,IDictPublicDal{} public partial interface IDictPublicService : IBaseService<DictPublic>{} public partial class DictPublicService : BaseService<DictPublic>,IDictPublicService { private IDictPublicDal Dal = new DictPublicDal(); public override void SetCurrentDal() { CurrentDal = new DictPublicDal(); } } /// <summary> /// DictPublic 控制器 /// </summary> [ApiController] public class DictPublicController : ControllerBase { #region 定义 /// <summary> /// IDictPublicService DictPublicService /// </summary> private IDictPublicService DictPublicService; /// <summary> /// 初始化服务信息 /// </summary> /// <param name="dictPublicService">IDictPublicService dictPublicService</param> public DictPublicController(IDictPublicService dictPublicservice) { DictPublicService = dictPublicService; } #endregion } |
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
2017-11-22 oracle数据库使用小结