博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
步步为营-60-代码生成器
阅读量:5046 次
发布时间:2019-06-12

本文共 8696 字,大约阅读时间需要 28 分钟。

说明:代码生成器有很多,我们目前介绍的是动软代码生成器

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;   }#>
自定义模板(字段的各个属性,包括字段类型在C#和数据库之间的转化)

 

转载于:https://www.cnblogs.com/YK2012/p/6877190.html

你可能感兴趣的文章
我有一个 APP 创意,如何将其实现?
查看>>
SVN改地址eclipse怎么同步
查看>>
Xadmin查询
查看>>
Jsf 页面导航Navigation总结
查看>>
android studio 2018.4.16 intent
查看>>
html4与html5的区别
查看>>
html复习
查看>>
cordova StatusBar插件的使用(设置手机状态栏颜色和页面头部颜色一致),做出和原生一样的页面效果体验...
查看>>
<button>和<input type="button"> 的区别
查看>>
编辑代码或者文档时光标变成了一闪一闪的方块怎么处理?
查看>>
Carthage的安装和使用
查看>>
iOS bug解决方案(01)
查看>>
TopCoder SRM 642 Div.2 1000 --二分+BFS
查看>>
UVALive 4426 Blast the Enemy! --求多边形重心
查看>>
http://www.cs.cmu.edu/~ggordon/IRLS-example/
查看>>
用上了360免费云盘
查看>>
jenkins document
查看>>
SQL2005安装图解
查看>>
C++的优秀特性4:指针
查看>>
grid调整宽度自动保存,下次启动保持原来的宽带
查看>>