• Entity Framework 4 使用T4模板生成实体


    数据库优先模式下,由于数据库命名和C#命名规范不同,所以感觉很别扭。

    首先,创建一个文件,命名随意,我使用了 EF.CS.Extend.ttinclude 其中方法主要是处理_

    <#@ assembly name="System.Core" #>
    <#@ import namespace="System" #>
    <#@ import namespace="System.Linq" #>
    <#@ import namespace="System.Text" #>
    <#@ import namespace="System.Collections.Generic" #>
    <#+
    public class CodeGenerationHelper
    {
       public String FilterUnderline(String code)
        {
            if (String.IsNullOrWhiteSpace(code)) return String.Empty;
            StringBuilder sb = new StringBuilder();
    
            String[] values = code.Split(new char[] { '_' });
            foreach (string value in values)
            {
                if(String.IsNullOrWhiteSpace(value)) continue;
                sb.Append(value.Substring(0, 1).ToUpper());
                if(value.Length > 1)
                    sb.Append(value.Substring(1, value.Length - 1).ToLower());
            }
    
            return sb.ToString();
        }
    }
    #>
    

    然后在生成数据库实体的头部加入 <#@ include file="EF.CS.Extend.ttinclude"#>

    在T4中的各个生成代码的位置加入过滤方法,这个比较烦,VS2010和VS2012所使用的生成模板不一样,所以具体需要看情况。

    最后看看生成的代码,哈哈

    [Table("Take_Office")]
    public partial class TakeOffice
    {
    
    	[Column("id")]
    	public long Id { get; set; }
    
    	[Column("pers_code")]
    	public string PersCode { get; set; }
    
    	[Column("if_public_code")]
    	public Nullable<short> IfPublicCode { get; set; }
    
    	[Column("if_public_name")]
    	public string IfPublicName { get; set; }
    
    	[Column("public_form_code")]
    	public Nullable<short> PublicFormCode { get; set; }
    
    	[Column("public_form_name")]
    	public string PublicFormName { get; set; }
    }
    

      

  • 相关阅读:
    3524: [Poi2014]Couriers -- 主席树
    bzoj 2190: [SDOI2008]仪仗队 -- 欧拉函数
    模板 -- 树链剖分
    bzoj 1823: [JSOI2010]满汉全席 -- 2-sat
    bzoj 1704: [Usaco2007 Mar]Face The Right Way 自动转身机 -- 贪心
    bzoj 1231: [Usaco2008 Nov]mixup2 混乱的奶牛 -- 状压DP
    redis 主从复制
    redis 事务
    redis持久化——AOF
    redis 持久化 ——RDB
  • 原文地址:https://www.cnblogs.com/warrior/p/2662014.html
Copyright © 2020-2023  润新知