说明:代码生成器有很多,我们目前介绍的是动软代码生成器
1 打开动软,连接数据库,新建项目,选择要生成的表和地址,生成
2 自定义模板生成
<#@ template language="c#" HostSpecific="True" #><#@ output extension= ".cs" #><# TableHost host = (TableHost)(Host); string ModelSpace = host.NameSpace+".Model."+ host.GetModelClass(host.TableName); string DALSpace= host.NameSpace+".DAL."+ host.GetDALClass(host.TableName); ColumnInfo identityKey=host.IdentityKey; string returnValue = "void"; if (identityKey!=null) { returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName); }#>using Git.Framework.Log;using System;using System.Collections.Generic;using System.Linq;using System.Text;using Git.Framework.DataTypes.ExtensionMethods;using Git.Framework.ORM;using Git.Storage.Entity.Store;using Git.Framework.DataTypes;using Git.Framework.Json;using Git.Storage.Common;using System.Net.Http;using Git.Framework.Resource;using System.Transactions;using Newtonsoft.Json.Linq;using Newtonsoft.Json;using Git.Storage.Provider.Base;using Git.Framework.Cache;using Git.Storage.Entity.<#= host.TableName #>;namespace Git.Storage.Provider.<#= host.TableName #>//ToDo:注意此处的命名空间{ public partial class <#= host.TableName #>Provider : DataFactory { private readonly Log log = Log.Instance(typeof(<#= host.TableName #>Provider)); public <#= host.TableName #>Provider() { } ////// 添加 /// /// ///public int Add(<#= host.TableName #>Entity entity) { entity.IncludeAll(); int line = this.<#= host.TableName #>.Add(entity); if (line > 0) { CacheHelper.Remove(CacheKey.JOOSHOW_<#= host.TableName.ToUpper() #>_CACHE); } return line; } /// /// 获得所有 /// ///public List<<#= host.TableName #>Entity> GetList() { List<<#= host.TableName #>Entity> listResult = CacheHelper.Get(CacheKey.JOOSHOW_<#= host.TableName.ToUpper() #>_CACHE) as List<<#= host.TableName #>Entity>; if (!listResult.IsNullOrEmpty()) { return listResult; } <#= host.TableName #>Entity entity = new <#= host.TableName #>Entity(); entity.IncludeAll(); listResult = this.<#= host.TableName #>.GetList(entity); if (!listResult.IsNullOrEmpty()) { CacheHelper.Insert(CacheKey.JOOSHOW_<#= host.TableName.ToUpper() #>_CACHE, listResult); } return listResult; } /// /// 根据编号获取 /// /// ///public <#= host.TableName #>Entity GetItem(string num) { List<<#= host.TableName #>Entity> listResult = GetList(); if (!listResult.IsNullOrEmpty()) { return listResult.FirstOrDefault(a => a.<#= host.TableName #>Num == num); } return null; } /// /// 分页获取 /// /// /// ///public List<<#= host.TableName #>Entity> GetPageList(<#= host.TableName #>Entity entity, ref PageInfo pageInfo) { List<<#= host.TableName #>Entity> listResult = GetList(); if (!listResult.IsNullOrEmpty()) { int rowCount = 0; rowCount = listResult.Where(a => a.<#= host.TableName #>Num.Contains(entity.<#= host.TableName #>Num) && a.IsDelete.Equals((int)EIsDelete.NotDelete)).Count(); pageInfo.RowCount = rowCount; return listResult.Where(a => a.<#= host.TableName #>Num.Contains(entity.<#= host.TableName #>Num) && a.IsDelete.Equals((int)EIsDelete.NotDelete)).OrderBy(a => a.<#= host.TableName #>Num).Skip((pageInfo.PageIndex - 1) * pageInfo.PageSize).Take(pageInfo.PageSize).ToList(); } return null; } /// /// 删除 /// /// ///public int Delete(string <#= host.TableName #>Num) { <#= host.TableName #>Entity entity = new <#= host.TableName #>Entity(); entity.Where(a => a.<#= host.TableName #>Num == <#= host.TableName #>Num); int line = this.<#= host.TableName #>.Delete(entity); if (line > 0) { CacheHelper.Remove(CacheKey.JOOSHOW_<#= host.TableName.ToUpper() #>_CACHE); } return line; } /// /// 编辑 /// /// ///public int Edit(<#= host.TableName #>Entity entity) { entity.Include(a => new { <#= GetAllCol(host.Fieldlist)#> }); entity.Where(a => a.<#= host.TableName #>Num == entity.<#= host.TableName #>Num); int line = this.<#= host.TableName #>.Update(entity); return line; } } }<#+ private string GetAllCol(List colInfoList){ string value = ""; foreach(ColumnInfo colInfo in colInfoList){ value +="a."+colInfo.ColumnName+", "; } value=value.Replace("a.ID,",""); //去掉ID value=value.Trim(); value=value.Substring(0,value.Length-1);//去掉最后一个, return value; }#>
注意自定义方法必须是文件的结尾,后面连空格都不能有
<#@ template language="c#" HostSpecific="True" #><#@ output extension= ".cs" #><# TableHost host = (TableHost)(Host); string ModelSpace = host.NameSpace+".Model."+ host.GetModelClass(host.TableName); string DALSpace= host.NameSpace+".DAL."+ host.GetDALClass(host.TableName); ColumnInfo identityKey=host.IdentityKey; string returnValue = "void"; if (identityKey!=null) { returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName); }#>using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using Git.Framework.ORM;//ToDo:注意此处的命名空间namespace Git.Storage.Entity.Base{ [TableAttribute(DbName = "<#= host.DbName #>", Name = "<#= host.TableName #>", PrimaryKeyName = "ID", IsInternal = false)] public partial class <#= host.TableName #>Entity:BaseEntity { public <#= host.TableName #>Entity() { } <# foreach (ColumnInfo c in host.Fieldlist) { #> [DataMapping(ColumnName = "<#= c.ColumnName #>", DbType = DbType.<#= GetType(c.TypeName)#>,Length=<#=c.Length#>,CanNull=<#= c.Nullable#>,DefaultValue=<#= GetDefalut(c.DefaultVal)#>,PrimaryKey=<#=c.IsPrimaryKey#>,AutoIncrement=<#=c.IsIdentity#>,IsMap=true)] public .<#= GetType(c.TypeName)#> <#= c.ColumnName #> { get; set; } public <#= host.TableName #>Entity Include<#= c.ColumnName #> (bool flag) { if (flag && !this.ColumnList.Contains("<#= c.ColumnName #>")) { this.ColumnList.Add("<#= c.ColumnName #>"); } return this; } <# } #> } } <#+ private string GetType(string name) { string value = "string"; switch (name) { case "int": value = "Int32"; break; case "bigint": value = "Int64"; break; case "bit": value = "Int16"; break; case "char": value = "String"; break; case "date": value = "DateTime"; break; case "datetime": value = "DateTime"; break; case "datetime2": value = "DateTime"; break; case "float": value = "Double"; break; case "money": value = "Double"; break; case "nchar": value = "String"; break; case "ntext": value = "String"; break; case "nvarchar": value = "String"; break; case "varchar": value = "String"; break; case "text": value = "String"; break; } return value; } private string GetDefalut(string colName){ string value = "null"; if(colName!=""){ value=colName; } return value; }#>