欢迎光临

木子屋

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

作者: dnawo

日期: 2012-12-28 03:15 PM

分类: 程序修改

内容: 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)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.730 ms

返回

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

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.730 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.764 ms

返回