自动生成的索引视图的自定义不起作用

Customization for autogenrated index view not working

我正在使用 MVC 4.0entity-framework 5 生成 model

我试图理解自定义的概念,我遵循了教程网站中给出的相同步骤,例如 this and this

唯一的区别是我在不同的子文件夹中添加此自定义 class 代码,因为当直接尝试将其添加到 模型文件夹 时,它显示错误 employee class 已在此创建(由实体框架自动生成)

Auto generated code By Entity Framework

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace CustomizationConceptUmang.Models
{
    using System;
    using System.Collections.Generic;

    public partial class employee
    {
        public int id { get; set; }
        public string name { get; set; }
        public string email { get; set; }
    }
}

Code for customization

namespace CustomizationConceptUmang.Models.ViewModel
{
    [MetadataType(typeof(employeeMetaData))]
    public partial class employee
    {
    }
    public class employeeMetaData
    {
        [Display(Name="Employee Name")]
        public string name { get; set; }
    }
}

View

<th>
     @Html.DisplayNameFor(model => model.name)
</th>

仍然显示 姓名 而不是 员工姓名

请建议我如何解决此问题提前致谢。

部分在不同的命名空间中,因此它们是两个独立的 class 并且元数据不会应用于 employee 实体。

匹配命名空间,将多个部分 class 合并为一个。

无论如何不要使用 Entity Framework 模型作为视图模型。为视图模型创建一个带有注释的单独 class,并映射到您的实体和从您的实体映射。以后你会感谢我的。