MVC DB首先修复显示名称

Eng*_*oon 12 asp.net-mvc database-first

我正在使用mvc 4,首先使用数据库.

每次刷新model.edmx文件时,都会删除我添加的显示名称属性.如何在刷新之间保持显示名称属性?

nas*_*ski 18

你想要使用System.ComponentModel.DataAnnotations.以下是EF中"用户"表的简单示例,向您展示如何:

namespace YourNamespace.BlaBlaBla
{
    [MetadataType(typeof(UserHelper))]
    public partial class User { }

    public class UserHelper
    {
        [Display(Name = "Your New Title For Name")]
        public string Name { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

您也可以在课程中包含验证.确保它是一个名为完全相同的部分类 - 也不要忘记它必须与.edmx完全相同的命名空间.


Rom*_*ias 6

您需要使用MetaDataTypes模型..

[MetadataType(typeof(ModelMD))]
public partial class Model
{
//This is for "extending" the EF generated model, saying what class is used for metadata, in your case DisplayName
}

public partial class ModelMD
{

    [Display(Name = "Model_Title", ResourceType = typeof(DataFieldLabels))]
    public string Titulo { get; set; }

    [Display(Name = "Model_Description", ResourceType = typeof(DataFieldLabels))]
    public string Descripcion { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,我使用资源文件来获取字段显示名称...但您可以以更加强化的方式使用它:)

你应该在另一个文件夹中创建一个新文件,比如说"ModelMD".这样,一旦模型重新生成,该文件将保持不变.

重要说明:ModelMD文件应使用与原始模型相同的命名空间.如果将文件放在不同的文件夹中,则默认为另一个命名空间.