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

14

我们知道EF根据我们添加到.edmx文件中的表生成类。但它们不会有任何[DisplayName]数据注释。

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

EF生成的类

 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>

1
如果您遇到的问题是数据注释未显示在MVC 4中的部分类实体上,请尝试添加MetadataType并创建带有属性的部分类。请参考以下链接,并告诉我它是否有效:https://dev59.com/5W3Xa4cB1Zd3GeqPeGfR 和 https://dev59.com/IFbUa4cB1Zd3GeqPCdvc - NSGaga-mostly-inactive
我想你可以尝试使用POCO生成器。 - Alex Ovechkin
3个回答

14
使用元数据类,并通过MetadataTypeAttribute将其附加到实体类上。您可以在元数据类中的属性上指定数据注释属性(但不要为这些属性实现代码)。
MSDN:http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx 编辑:一个初步的注意事项是定义用于附加MetadataTypeAttribute的部分类的名称空间。请确保将其命名空间更改为与原始实体使用的命名空间相同,以便它定义相同的类。

好答案 - 而且随后的陷阱也很准确! - Daniël Camps

5
您可以更改模板.tt文件,该文件会生成用于使用模型文档属性生成必要属性的类代码。 例如,对于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
请注意,生成的类是一个部分类。因此,您可以创建另一个同名的部分类,并在其上进行注释。然后,如果您更改.edmx文件,则第二个部分类将不会刷新。
更多关于MVC DB first Fix display Name的信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接