不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
Entity Framework Power Tools Beta 2模板Entity.tt[修改版]
编辑:dnawo 日期:2012-12-28
Entity.tt:
修改说明
·构造函数中增加对属性初始化;
·为属性增加Required、StringLength和Display特性;
复制内容到剪贴板 程序代码
<#@ 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; }
<#
}
}
#>
}
}
<#@ 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特性;
上一篇: Visual Studio 2012附加到进程调试Web应用程序示例
下一篇: 国家统计局县及县以上行政区划导入SQL Server步骤
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
下一篇: 国家统计局县及县以上行政区划导入SQL Server步骤
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 0 | 引用: 0 | 查看次数: 6059
发表评论
请登录后再发表评论!