我在项目中使用EF 4.1“数据库优先”。使用T4模板,我有条件地使用Required,DisplayName,MaxLength等属性来装饰我生成的属性。在我的T4模板时,写属性方法里面,我有这样的:EF T4模板 - 有条件地发射自定义属性
var maxLength = edmProperty.TypeUsage.Facets.Where(f => f.Name == "MaxLength").FirstOrDefault();
if (maxLength != null && maxLength.Value.ToString() != "Max")
{#>
[MaxLength(<#= maxLength.Value #>, ErrorMessage = "<#=edmProperty.Documentation != null ? edmProperty.Documentation.LongDescription : edmProperty.Name #> cannot exceed <#=maxLength.Value #> characters.")]
<#+}
这个伟大工程,可以从EDMX推断出的信息,但也有一些事情是定制的,只是不可用。
作为示例,假设我想用自定义正则表达式类型属性修饰EmailAddress
属性。如何自动“选择”我的模型中的电子邮件地址字段并应用此属性而不检查属性名称?
有没有办法“扩展”EdmProperty或者添加自定义属性,如IsEmailAddress?
谢谢!
这是不正确的。查看我的答案了解更多详情 – Pawel 2013-03-12 16:38:01