Search code examples
asp.net-mvcdatabase-first

MVC DB first Fix display Name


I'm using mvc 4, with database first.

Everytime I refresh the model.edmx file, the display name attribute I added is removed. How can I the keep display name attribute between refreshes?


Solution

  • 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.