Jam*_*123 12 asp.net-mvc entity-framework ef-database-first asp.net-mvc-4
我们知道EF根据我们添加到.edmx文件的表生成类.哪个不会有[DisplayName] DataAnnotations.
如何在不修改生成的类的情况下添加此[DisplayName]?因为生成的类可以被覆盖如果我修改.edmx文件(重新添加修改的表),如果数据库更改.所以我不想修改生成类本身.
public partial class Committee
{
public string Committee_Description { get; set; }
public byte[] Committee_Id { get; set; }
public string Rn_Descriptor { get; set; }
public Nullable<System.DateTime> Rn_Create_Date { get; set; }
......
.....
Run Code Online (Sandbox Code Playgroud)
<tr>
<th>
@Html.DisplayNameFor(model => model.Item2.GetEnumerator().Current.Committee_Name)
</th>
Run Code Online (Sandbox Code Playgroud)
Moh*_*oho 14
使用元数据类并通过MetadataTypeAttribute将其附加到您的实体类.您可以在元数据类的属性上指定数据注释属性(不是所述属性的实现).
MSDN:http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx
编辑:初始问题是您定义用于附加MetadataTypeAttribute的分部类的命名空间.确保将其名称空间更改为原始实体使用的名称空间,以便它定义相同的类.
小智 5
您可以更改模板.tt文件,生成类代码以使用模型的Documentation属性生成具有nessesary属性的类.例如,对于EF5,您可以CodeStringGenerator.Property()通过以下方式替换*Model.tt方法:
public string Property(EdmProperty edmProperty)
{
return string.Format(
CultureInfo.InvariantCulture,
"{5} {0} {1} {2} {{ {3}get; {4}set; }}",
Accessibility.ForProperty(edmProperty),
_typeMapper.GetTypeName(edmProperty.TypeUsage),
_code.Escape(edmProperty),
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
_code.SpaceAfter(Accessibility.ForSetter(edmProperty)),
(edmProperty.Documentation == null ? "" : ("[Display(Name=\""+edmProperty.Documentation.Summary+"\")]"+Environment.NewLine+" ")));
}
Run Code Online (Sandbox Code Playgroud)
并CodeStringGenerator.UsingDirectives()与:
public string UsingDirectives(bool inHeader, bool includeCollections = true)
{
return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
? string.Format(
CultureInfo.InvariantCulture,
"{0}using System;{1}" +
"{2}{3}",
inHeader ? Environment.NewLine : "",
includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
inHeader ? "" : Environment.NewLine,"using System.ComponentModel.DataAnnotations;"+Environment.NewLine)
: "";
}
Run Code Online (Sandbox Code Playgroud)
Documentation.Summary在模型和模板中设置属性后,.tt将生成具有适当属性的所有类,而不使用元数据类并通过MetadataTypeAttribute将其附加到您的实体类.例如:
namespace DataAdmin.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Discount
{
public Discount()
{
this.DiscountPeriods = new HashSet<DiscountPeriod>();
this.Clinics = new HashSet<Clinic>();
this.Doctors = new HashSet<Doctor>();
this.Servs = new HashSet<Serv>();
}
public int DiscountKey { get; set; }
[Display(Name="Discount name")]
public string Name { get; set; }
public string MisCode { get; set; }
public string MisName { get; set; }
public string MisDesc { get; set; }
public decimal Perc { get; set; }
public int Rang { get; set; }
public Nullable<int> DiscountTypeKey { get; set; }
public virtual ICollection<DiscountPeriod> DiscountPeriods { get; set; }
public virtual ICollection<Clinic> Clinics { get; set; }
public virtual ICollection<Doctor> Doctors { get; set; }
public virtual ICollection<Serv> Servs { get; set; }
public virtual DiscountType DiscountType { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14414 次 |
| 最近记录: |