如何更改EF生成的类属性的[DisplayName]?

时间:2013-03-25 18:58:35

标签: asp.net-mvc entity-framework asp.net-mvc-4 ef-database-first

我们知道EF根据我们添加到.edmx文件的表生成类。哪个不会有[DisplayName] DataAnnotations。

如何在不修改生成的类的情况下添加此[DisplayName]?因为生成的类可以被覆盖如果我修改.edmx文件(重新添加修改后的表),如果数据库更改。所以我不想修改生成类本身。

EF Generated Class

 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; }
       ......
       .....

视图

 <tr>
            <th>
                @Html.DisplayNameFor(model => model.Item2.GetEnumerator().Current.Committee_Name)
            </th>

3 个答案:

答案 0 :(得分:12)

使用元数据类并通过MetadataTypeAttribute将其附加到您的实体类。您可以在元数据类的属性上指定数据注释属性(不是所述属性的实现)。

MSDN:http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx

编辑:初始问题是您定义用于附加MetadataTypeAttribute的分部类的命名空间。请务必将其名称空间更改为原始实体使用的名称空间,以便它定义相同的类。

答案 1 :(得分:5)

您可以更改模板.tt文件,生成类代码以使用模型的Documentation属性生成具有nessesary属性的类。 例如,对于EF5,您可以在* Model.tt方法CodeStringGenerator.Property()中替换为:

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+"   ")));
}

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)
        : "";
}

在模型和模板之后设置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; }
    }
}

答案 2 :(得分:2)

请注意,生成的类是一个部分类。因此,您可以创建另一个具有相同名称的分部类,并对其进行注释。如果更改.edmx文件,则不会刷新第二个分部类。

更多关于MVC DB first Fix display Name