Display name in Data Entity framework

后端 未结 2 379
谎友^
谎友^ 2021-01-19 05:55

I\'d like to know how to change the display name of a model, and customize error messages in Entity Framework. I tried the following but it didn\'t work.

            


        
相关标签:
2条回答
  • 2021-01-19 06:02

    First you need to reference this:

    using System.ComponentModel.DataAnnotations;
    

    For changing the display name of the column, actually [Display(Name="Name Agency")] is OK. I'm using it in my projects.

    For error message

    [Required(ErrorMessage="Required...")]
    

    I read that it is possible that this won't work if you are using the entity framework designer because the designer overwrites your changes over and over then you will need to use the metadatatype something like this:

    [MetadataType(typeof(MetadataMyClass))]
    public partial class myclass
    {
    }
    
    //data annotations here
    public class MetadataMyClass
    {
      [Required(ErrorMessage = "Required...")]
      [Display(Name="Column Name")]
      public global:: System.String Nag
      {
        // ... etc, etc...
      }
    }
    
    0 讨论(0)
  • 2021-01-19 06:05

    Change [Display(Name = "Name Agency")] to [DisplayName("Name Agency")] instead.

    0 讨论(0)
提交回复
热议问题