废话不多说,下面我们来生成一个简单的实体类的例子(我用的是6.5的),目标代码如下:
   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/// <summary>
/// author:lanzhaoyi
/// createtime:2012-12-03 13:08:17
/// </summary>
namespace LZ_MIS.Entity{
     
     /// <summary>
     /// 
     /// </summary>
     public partial class Role    {        
        
        /// <summary>
        /// 
        /// </summary>
        public uint ID{ get;set; }        
        
        /// <summary>
        /// 
        /// </summary>
        public string Name{ get;set; }        
        
        /// <summary>
        /// 
        /// </summary>
        public string Remark{ get;set; }        
     }
}

步骤如下:

    1、创建一个模版

         鼠标单击File->New->CShareTemplate

         这样我们就创建了一个C#模版,模版代码如下:

       

<%-- 
Name:
Author: 
Description: 
--%>
<%@ Template Language="C#" TargetLanguage="Text" %>
<%@ Property Name="SampleStringProperty" Default="SomeValue" Type="System.String" %>
<%@ Property Name="SampleBooleanProperty" Default="True" Type="System.Boolean" %>
My static content here.
My dynamic content here: "<%= SampleStringProperty %>"
Call a script method: <%= SampleMethod() %>
<% if (SampleBooleanProperty) { %>
My conditional content here.
<% } %>
<script runat="template">
// My methods here.
public string SampleMethod()
{
  return "Method output.";
}
</script>

Template:模版标间   Language  模版语言     TargetLanguage  生成文件的语言                                        注:此标签是必须的,它告诉我们了模版的基本信息

Property  属性          Name   属性名称           Default  属性对应的默认值                 Type  属性的类型     注:声明此属性后可以在右边属性选项卡中可能到

<%%> 之间是我们要用来生成代码的脚本,如果为在<%%>的内容会直接原样输出的

<%=变量或方法%>   它可以直接输出变量的值或方法的返回值 

2、加载数据库的程序集与命名空间

    我们在生成数据库表对应的实体类的时候,我们需要连接上对应的数据库。这个时候我们就需要在模版中加载对应的内容

    首先我们将

   

<%-- 
      Name:
      Author: 
      Description:
     --%>

     <%@ Template Language="C#" TargetLanguage="Text" %>保留,其它没用的都删掉,加上对应的程序集与命名空间:
     <%@ Assembly Name="SchemaExplorer" %>
     <%@ Import Namespace="SchemaExplorer" %>


3、声明属性

    现在我们要只对对应的生成那个哪个表对应的实体类和实体类存放在哪个命名空间下

<%@ Property Name="Table" Type="TableSchema"%>
<%@ Property Name="NameSpace" Type="String" Category="Content" %>

4、实现实体类的生成

   这一步是我们生成实体类的主要部分,见代码


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/// <summary>
/// author:lanzhaoyi
/// createtime:<%= DateTime.Now %>
/// </summary>
namespace <%=NameSpace %>.Entity{
     
     /// <summary>
     /// <%=Table.Description %>
     /// </summary>
     public partial class <%= DelHead(Table.Name)%>    {        
        <% foreach(ColumnSchema col in Table.Columns){ %>
        
        /// <summary>
        /// <%= col.Description %>
        /// </summary>
        public <%= DataType2CSharpType(col.DataType) %> <%=DelHeadOne(col.Name) %>{ get;set; }        
        <% } %>    
     }
}



NameSpace 则是我们上面定义的属性,测试的时候我们可以在右边的属性选项卡中初始赋值

Table 也是我们对表的属性定义,在右边属性选项卡中可初始赋值

Table.Description 表的描述(我用的是MYSQl在输出表描述和字段描述时候始终是空,大家有好的方法给我说下谢谢)

DelHead 和DataType2CSharpType则是两个方法,用于处理字符串和类型转换,代码:


/// <summary>
    ///把数据库类型转化为C#类型
    /// </summary>
    public string DataType2CSharpType(System.Data.DbType dbType)
    {
        switch (dbType)
        {
            case DbType.AnsiString:
                return "string";
            case DbType.AnsiStringFixedLength:
                return "string";
            case DbType.Binary:
                return "byte[]";
            case DbType.Boolean:
                return "bool";
            case DbType.Byte:
                return "byte";
            case DbType.Currency:
                return "decimal";
            case DbType.Date:
                return "DateTime";
            case DbType.DateTime:
                return "DateTime";
            case DbType.DateTime2:
                return "DateTime";
            case DbType.DateTimeOffset:
                return "DateTime";
            case DbType.Decimal:
                return "decimal";
            case DbType.Double:
                return "double";
            case DbType.Guid:
                return "Guid";
            case DbType.Int16:
                return "short";
            case DbType.Int32:
                return "int";
            case DbType.Int64:
                return "long";
            case DbType.Object:
                return "object";
            case DbType.SByte:
                return "sbyte";
            case DbType.Single:
                return "float";
            case DbType.String:
                return "string";
            case DbType.StringFixedLength:
                return "string";
            case DbType.Time:
                return "DateTime";                
            case DbType.UInt16:
                return "ushort";
            case DbType.UInt32:
                return "uint";
            case DbType.UInt64:
                return "ulong";
            case DbType.VarNumeric:
                return "decimal";
            case DbType.Xml:
                return "string";
            default:
                return "object";
        }
    }
    /// <summary>
    /// 字符串转化为小写
    /// </summary>
    public string ToLowercase(string str)
	{
		str = str.ToLower();	
		return str;
	}
	
	/// <summary>
    /// 泛型
    /// </summary>
	public string ToGeneric(string str)
	{
		str = "<" + str + ">";	
		return str;
	}
    ///因为我的所有表前面都加了XX_ 字段前面都加了L_所以我要把前缀去掉,且首字母大写    
    public string ToHeadUpper(string str){
        str =  str.Replace(str.Substring(0,str.IndexOf('_')+2),str.Substring(0,str.IndexOf('_')+2).ToUpper());
        return str;
    }
    
    public string DelHead(string str){
        str =  str.Replace(str.Substring(0,str.IndexOf('_')+2),str.Substring(0,str.IndexOf('_')+2).ToUpper());
        str = str.Substring(str.IndexOf('_')+1);
        return str;
    }
    
    public string DelHeadOne(string str){
        str =  str.Replace(str.Substring(0,str.IndexOf('_')+1),str.Substring(0,str.IndexOf('_')+1).ToUpper());
        str = str.Substring(str.IndexOf('_')+1);
        return str;
    }
</script>



好了,我们所有的代码都写完了,然后我们点击上方的“Generate”按钮测试一下,就是生成对应表的实体层代码

完整代码如下:


<%-- 
Name:实体类
Author:Description:用于动态创建实体类 
--%>
<%@ Template Language="C#" TargetLanguage="C#" %>

<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>

<%@ Property Name="Table" Type="TableSchema"%>
<%@ Property Name="NameSpace" Type="String" Category="Content" %>

<script runat="template">
    /// <summary>
    ///把数据库类型转化为C#类型
    /// </summary>
    public string DataType2CSharpType(System.Data.DbType dbType)
    {
        switch (dbType)
        {
            case DbType.AnsiString:
                return "string";
            case DbType.AnsiStringFixedLength:
                return "string";
            case DbType.Binary:
                return "byte[]";
            case DbType.Boolean:
                return "bool";
            case DbType.Byte:
                return "byte";
            case DbType.Currency:
                return "decimal";
            case DbType.Date:
                return "DateTime";
            case DbType.DateTime:
                return "DateTime";
            case DbType.DateTime2:
                return "DateTime";
            case DbType.DateTimeOffset:
                return "DateTime";
            case DbType.Decimal:
                return "decimal";
            case DbType.Double:
                return "double";
            case DbType.Guid:
                return "Guid";
            case DbType.Int16:
                return "short";
            case DbType.Int32:
                return "int";
            case DbType.Int64:
                return "long";
            case DbType.Object:
                return "object";
            case DbType.SByte:
                return "sbyte";
            case DbType.Single:
                return "float";
            case DbType.String:
                return "string";
            case DbType.StringFixedLength:
                return "string";
            case DbType.Time:
                return "DateTime";                
            case DbType.UInt16:
                return "ushort";
            case DbType.UInt32:
                return "uint";
            case DbType.UInt64:
                return "ulong";
            case DbType.VarNumeric:
                return "decimal";
            case DbType.Xml:
                return "string";
            default:
                return "object";
        }
    }

    /// <summary>
    /// 字符串转化为小写
    /// </summary>
    public string ToLowercase(string str)
	{
		str = str.ToLower();	
		return str;
	}
	
	/// <summary>
    /// 泛型
    /// </summary>
	public string ToGeneric(string str)
	{
		str = "<" + str + ">";	
		return str;
	}
    
    public string ToHeadUpper(string str){
        str =  str.Replace(str.Substring(0,str.IndexOf('_')+2),str.Substring(0,str.IndexOf('_')+2).ToUpper());
        return str;
    }
    
    public string DelHead(string str){
        str =  str.Replace(str.Substring(0,str.IndexOf('_')+2),str.Substring(0,str.IndexOf('_')+2).ToUpper());
        str = str.Substring(str.IndexOf('_')+1);
        return str;
    }
    
    public string DelHeadOne(string str){
        str =  str.Replace(str.Substring(0,str.IndexOf('_')+1),str.Substring(0,str.IndexOf('_')+1).ToUpper());
        str = str.Substring(str.IndexOf('_')+1);
        return str;
    }
</script>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/// <summary>
/// author:lanzhaoyi
/// createtime:<%= DateTime.Now %>
/// </summary>
namespace <%=NameSpace %>.Entity{
     
     /// <summary>
     /// <%=Table.Description %>
     /// </summary>
     public partial class <%= DelHead(Table.Name)%>    {        
        <% foreach(ColumnSchema col in Table.Columns){ %>
        
        /// <summary>
        /// <%= col.Description %>
        /// </summary>
        public <%= DataType2CSharpType(col.DataType) %> <%=DelHeadOne(col.Name) %>{ get;set; }        
        <% } %>    
     }
}