MVC DB first Fix display Name

浪尽此生 提交于 2019-12-03 06:42:32

You are going to want to use System.ComponentModel.DataAnnotations. Here is a simplistic Example for a 'User' table in EF to show you how:

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

You can also include validation in your class as well. Be sure it is a partial class named the exact same - also do not forget that it must be in the exact same namespace as your .edmx.

You need to use MetaDataTypes models..

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

In the above example I'm using Resource Files to get the Fields display names... but you could use it in a more harcoded way :)

You should create a new file in another folder, let's say "ModelMD". That way, once the models are regenerated this file is kept unchanged.

Important: The ModelMD file should use the same Namespace that the original model. If you put the file in a different folder it defaults to another namespace.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!