Entity Framework Power Tools Beta 2模板Entity.tt[修改版]

Entity.tt:
<#@ template hostspecific="true" language="C#" #>
<#@ include file="EF.Utility.CS.ttinclude" #><#@
output extension=".cs" #><#

    var efHost = (EfTextTemplateHost)Host;
    var code = new CodeGenerationTools(this);
#>
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace <#= code.EscapeNamespace(efHost.Namespace) #>
{
    public class <#= efHost.EntityType.Name #>
    {
        public <#= code.Escape(efHost.EntityType) #>()
        {
<#
    var collectionNavigations = efHost.EntityType.NavigationProperties.Where(
        np => np.DeclaringType == efHost.EntityType
            && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many);

    // Add a ctor to initialize any collections
    if (collectionNavigations.Any())
    {
        foreach (var navProperty in collectionNavigations)
        {
#>
            this.<#= code.Escape(navProperty) #> = new List<<#= code.Escape(navProperty.ToEndMember.GetEntityType()) #>>();
<#
        }
    }

    //initialize Properties
    string fieldtype = "";
    string filedvalue = "";
    foreach (var property in efHost.EntityType.Properties)
    {
            fieldtype = code.Escape(property.TypeUsage).ToLower();
            if(fieldtype.IndexOf("byte") != -1 || fieldtype.IndexOf("int") != -1 || fieldtype.IndexOf("decimal") != -1 || fieldtype.IndexOf("double") != -1)
                filedvalue = "0";
            else if(fieldtype.IndexOf("bool") != -1)
                filedvalue = "false";
            else if(fieldtype.IndexOf("string") != -1)
                filedvalue = "\"\"";
            else if(fieldtype.IndexOf("datetime") != -1)
                filedvalue = "DateTime.Now";
            else
                filedvalue = "0";
#>
            this.<#= code.Escape(property) #> = <#= filedvalue #>;
<#
    }
#>
        }
<#  
    foreach (var property in efHost.EntityType.Properties)
    {
#>
        /// <summary>
        /// <#= code.Escape(property) #>
        /// </summary>
<#
    var type = (PrimitiveType)property.TypeUsage.EdmType;
    if (type.ClrEquivalentType == typeof(string)
            || type.ClrEquivalentType == typeof(byte[]))
        {
            if (!property.Nullable)
            {
#>
        [Required(ErrorMessage = "*")]
<#
            }
                
            var maxLengthFacet = (Facet)property.TypeUsage.Facets.SingleOrDefault(f => f.Name == "MaxLength");
            if(maxLengthFacet != null && !maxLengthFacet.IsUnbounded)
            {
#>
        [StringLength(<#= maxLengthFacet.Value #>, ErrorMessage = "<=<#= maxLengthFacet.Value #>")]
<#
            }
        }
#>
        [Display(Name = "<#= code.Escape(property) #>")]
        <#= Accessibility.ForProperty(property) #> <#= code.Escape(property.TypeUsage) #> <#= code.Escape(property) #> { get; set; }
<#
    }

    foreach (var navProperty in efHost.EntityType.NavigationProperties.Where(np => np.DeclaringType == efHost.EntityType))
    {
        if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
        {
#>
        public virtual ICollection<<#= code.Escape(navProperty.ToEndMember.GetEntityType()) #>> <#= code.Escape(navProperty) #> { get; set; }
<#
        }
        else
        {
#>
        public virtual <#= code.Escape(navProperty.ToEndMember.GetEntityType()) #> <#= code.Escape(navProperty) #> { get; set; }
<#
        }
    }
#>
    }
}

修改说明

·构造函数中增加对属性初始化;
·为属性增加Required、StringLength和Display特性;

评论: 0 | 引用: 0 | 查看次数: 5901
发表评论
登录后再发表评论!