需要用到SqlSugar.dll
T4模板代码
<#@ template debug="false" hostspecific="true" language="C#" #> <#@ import namespace="System.Collections.Generic" #> <#@ assembly name="System.Core" #> <#@ assembly name="System.Xml" #> <#@ assembly name="System.Xml.Linq" #> <#@ assembly name="System.Data" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="SqlSugar" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Web" #> <#@ assembly name="$(SolutionDir)\packages\sqlSugar.3.5.3.4\lib\net40\SqlSugar.dll" #> using System; using System.ComponentModel.DataAnnotations; namespace IpFolder.Tms.Models { <# using (var db = new SqlSugarClient("Data Source=DESKTOP-4AD5QRH\\MYMSSQLSERVER;Integrated Security=False;User ID=sa;Password=123.a;Initial Catalog=DB_temp1;")) { foreach(var table in db.ClassGenerating.GetTableNames(db)) { #> public partial class <#=table#> { public int row_index { get; set; } <# foreach(var column in db.ClassGenerating.GetTableColumns(db, table)) { if((int)column.IS_PRIMARYKEY == 1) { #> [Key] <# } if((int)column.IS_NULLABLE == 0) { #> [Required] <# } if(ToType((string)column.DATA_TYPE, (int)column.IS_NULLABLE == 1) == "string") { #> [MaxLength(<#=((short)column.CHARACTER_MAXIMUM_LENGTH)/2#>)] <# } if(column.COLUMN_DESCRIPTION != null) { var descriptions = ((string)column.COLUMN_DESCRIPTION).Split(';'); if(descriptions.Length > 2) { if(!string.IsNullOrEmpty(descriptions[2])) { column.DATA_TYPE = descriptions[2]; } } if(descriptions.Length > 1) { var attributes = descriptions[1]; if(!string.IsNullOrEmpty(attributes)) { #> <#=attributes#> <# } } if(!string.IsNullOrEmpty(descriptions[0])) { if(table.ToString()=="SyncOrg"||table.ToString()=="SyncUser") { #> /// <summary> /// <#=descriptions[0]#> /// </summary> <# } else { #> [Display(Name = "<#=descriptions[0]#>")] <# } } } #> public <#=ToType((string)column.DATA_TYPE, (int)column.IS_NULLABLE == 1)#> <#=column.COLUMN_NAME#> { get; set; } <# } #> } <# } }; #> } <#+ public string ToType(string sqlType, bool nullable) { var nullableType = nullable ? "?" : string.Empty; switch (sqlType) { case "bit": return "bool" + nullableType; case "int": return "int" + nullableType; case "bigint": return "long" + nullableType; case "money": return "decimal" + nullableType; case "nchar": case "nvarchar": case "char": case "varchar": return "string"; case "uniqueidentifier": return "Guid" + nullableType; case "date": case "datetime": case "datetime2": return "DateTime" + nullableType; default: return sqlType + nullableType; } } #>
...
...
...
...