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

本文介绍了一种使用Camel规则、Pascal规则及原生方式遍历数据库所有表并生成PetaPoco ORM映射的方法,旨在帮助开发者快速实现数据库表到对象模型的映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在上一篇的版本中,我们使数据库中的单个表 生成 PetaPoco 构架下的 ORM 映射,这次呢,要使数据库中的所有的表 生成 PetaPoco 构架下的 ORM 映射。

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

<p><%-- 
Name:  Copyright © Sun 2013-2014 All rights reserved  
Contact me:  <a target=_blank href="mailto:Sunnydayhu@163.com">Sunnydayhu@163.com</a>
Author:  SpringFileld
Description: 遍历数据库中所有的表,并映射成 PetaPoco类的 orm 
DateTime: 2014-07-31
--%></p><p><%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="SourceData" Type="SchemaExplorer.DatabaseSchema" 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" %></p><p><%foreach( var tb in SourceData.Tables){ %></p><p>    [TableName("<%=tb.Name %>")]
    <%foreach (var pk in tb.PrimaryKey.MemberColumns){ %>
    [PrimaryKey("<%= pk.Name%>")]
    <%} %>
    [ExplicitColumns]
    public partial class  <%=tb.Name %> 
      {
        <%foreach( var cl in tb.Columns) {%>  
         [Column]
         public <%=CSharpAlias[cl.SystemType.FullName]%> <%=StringUtil.ToCamelCase(cl.Name)%> { get; set; } 
        <%} %>
      }
        
    
<%} %></p>


Pascall 规则:

<%-- 
Name:  Copyright © Sun 2013-2014 All rights reserved  
Contact me:  Sunnydayhu@163.com
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="SourceData" Type="SchemaExplorer.DatabaseSchema" 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" %>

<%foreach( var tb in SourceData.Tables){ %>

    [TableName("<%=tb.Name %>")]
    <%foreach (var pk in tb.PrimaryKey.MemberColumns){ %>
    [PrimaryKey("<%= pk.Name%>")]
    <%} %>
    [ExplicitColumns]
    public partial class  <%=tb.Name %> 
      {
        <%foreach( var cl in tb.Columns) {%>  
         [Column]
         public <%=CSharpAlias[cl.SystemType.FullName]%> <%=StringUtil.ToPascalCase(cl.Name)%> { get; set; } 
        <%} %>
      }
        
    
<%} %>


原生的:

<%-- 
Name:  Copyright © Sun 2013-2014 All rights reserved  
Contact me:  Sunnydayhu@163.com
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="SourceData" Type="SchemaExplorer.DatabaseSchema" 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" %>

<%foreach( var tb in SourceData.Tables){ %>

    [TableName("<%=tb.Name %>")]
    <%foreach (var pk in tb.PrimaryKey.MemberColumns){ %>
    [PrimaryKey("<%= pk.Name%>")]
    <%} %>
    [ExplicitColumns]
    public partial class  <%=tb.Name %> 
      {
        <%foreach( var cl in tb.Columns) {%>  
         [Column]
         public <%=CSharpAlias[cl.SystemType.FullName]%> <%=cl.Name%> { get; set; } 
        <%} %>
      }
        
    
<%} %>


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值