代码生成器 CodeSmith 的使用(六)

在上一篇的版本中,我们生成了数据库所有表中的字段,如果要使数据库中的单个表 生成 PetaPoco 构架下的 ORM 映射,使那怎么办。这是这篇博客的主要内容。







首先来看完整的 Camel 规则模板:



<%--

Name:  Copyright © Sun 2013-2014 All rights reserved

Contact me:  [email protected]

Author:  SpringFileld

Description: 遍历数据库中指定的表,并映射成 PetaPoco类的 orm

DateTime: 2014-07-31

--%>



<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>

<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>

<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="SchemaExplorer" %>

<%@ Assembly  Name="xftwl.Infrastructure" %>

<%@ Import  Namespace="xftwl.Infrastructure" %>





[TableName("<%=SourceTable.Name %>")]

<%foreach (var pk in SourceTable.PrimaryKey.MemberColumns){ %>

[PrimaryKey("<%= pk.Name%>")]

<%} %>

[ExplicitColumns]

public partial class  <%=SourceTable.Name %>

  {

    <%foreach( var cl in SourceTable.Columns) {%>

     [Column]

     public <%=CSharpAlias[cl.SystemType.FullName]%> <%=StringUtil.ToCamelCase(cl.Name) %> { get; set; }

    <%} %>

  }







生成的效果如下:





[TableName("acc_card_account")]

[PrimaryKey("Id")]

[ExplicitColumns]

public partial class  acc_card_account

  {

     [Column]

     public int id { get; set; }

     [Column]

     public System.DateTime dateCreate { get; set; }

     [Column]

     public string card { get; set; }

     [Column]

     public decimal balance { get; set; }

     [Column]

     public decimal interest { get; set; }

     [Column]

     public decimal interestRates { get; set; }

     [Column]

     public string remark { get; set; }

  }





Pascall 规则:



<%--

Name:  Copyright © Sun 2013-2014 All rights reserved

Contact me:  [email protected]

Author:  SpringFileld

Description: 遍历数据库中指定的表,并映射成 PetaPoco类的 orm

DateTime: 2014-07-31

--%>



<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>

<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>

<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="SchemaExplorer" %>

<%@ Assembly  Name="xftwl.Infrastructure" %>

<%@ Import  Namespace="xftwl.Infrastructure" %>





[TableName("<%=SourceTable.Name %>")]

<%foreach (var pk in SourceTable.PrimaryKey.MemberColumns){ %>

[PrimaryKey("<%= pk.Name%>")]

<%} %>

[ExplicitColumns]

public partial class  <%=SourceTable.Name %>

  {

    <%foreach( var cl in SourceTable.Columns) {%>

     [Column]

     public <%=CSharpAlias[cl.SystemType.FullName]%> <%=StringUtil.ToPascalCase(cl.Name) %> { get; set; }

    <%} %>

  }







生成的效果如下:



[TableName("acc_card_account")]

[PrimaryKey("Id")]

[ExplicitColumns]

public partial class  acc_card_account

  {

     [Column]

     public int Id { get; set; }

     [Column]

     public System.DateTime DateCreate { get; set; }

     [Column]

     public string Card { get; set; }

     [Column]

     public decimal Balance { get; set; }

     [Column]

     public decimal Interest { get; set; }

     [Column]

     public decimal InterestRates { get; set; }

     [Column]

     public string Remark { get; set; }

  }





 原生的:



<%--

Name:  Copyright © Sun 2013-2014 All rights reserved

Contact me:  [email protected]

Author:  SpringFileld

Description: 遍历数据库中指定的表,并映射成 PetaPoco类的 orm

DateTime: 2014-07-31

--%>



<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>

<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>

<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="SchemaExplorer" %>

<%@ Assembly  Name="xftwl.Infrastructure" %>

<%@ Import  Namespace="xftwl.Infrastructure" %>





[TableName("<%=SourceTable.Name %>")]

<%foreach (var pk in SourceTable.PrimaryKey.MemberColumns){ %>

[PrimaryKey("<%= pk.Name%>")]

<%} %>

[ExplicitColumns]

public partial class  <%=SourceTable.Name %>

  {

    <%foreach( var cl in SourceTable.Columns) {%>

     [Column]

     public <%=CSharpAlias[cl.SystemType.FullName]%> <%=cl.Name %> { get; set; }

    <%} %>

  }





生成的效果:



[TableName("acc_card_account")]

[PrimaryKey("Id")]

[ExplicitColumns]

public partial class  acc_card_account

  {

     [Column]

     public int Id { get; set; }

     [Column]

     public System.DateTime DateCreate { get; set; }

     [Column]

     public string Card { get; set; }

     [Column]

     public decimal Balance { get; set; }

     [Column]

     public decimal Interest { get; set; }

     [Column]

     public decimal InterestRates { get; set; }

     [Column]

     public string Remark { get; set; }

  }


 

你可能感兴趣的:(code)