【问题标题】:How to change [DisplayName] of EF generated class properties?如何更改 EF 生成的类属性的 [DisplayName]?
【发布时间】:2014-06-20 22:45:52
【问题描述】:

我们知道 EF 会根据我们添加到 .edmx 文件的表生成类。这不会为他们提供任何 [DisplayName] DataAnnotations。

如何在不修改它们的情况下添加生成的类的 [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>

【问题讨论】:

  • 尝试添加MetadataType - 并使用属性创建一个部分类 - 如果这是您面临的问题 - 看看这些链接 - 让我知道它是如何工作的 - stackoverflow.com/questions/14412468/… - @ 987654322@
  • 我猜你可以试试 POCO 生成器

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


【解决方案1】:

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

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

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

【讨论】:

  • 很好的答案 - 以及后续的陷阱!
【解决方案2】:

您可以更改模板 .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)
        : "";
}

在模型和模板.tt 中设置Documentation.Summary 属性之后,将生成具有适当属性的所有类,而不使用元数据类并通过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; }
    }
}

【讨论】:

    【解决方案3】:

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

    更多关于MVC DB first Fix display Name

    【讨论】:

      猜你喜欢
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      相关资源
      最近更新 更多