Using System.ComponentModel.DataAnnotations with Entity Framework 4.0

前端 未结 3 1660
北恋
北恋 2020-11-27 04:00

I\'m working with MVC3, and using Entity Framework 4.0 Entities as my model. So far, everything works great as far as using it as a model (all the crud operations/page gene

3条回答
  •  隐瞒了意图╮
    2020-11-27 04:26

    Like Austin Lamb's answer, but instead, nesting the MetaData class within the entity class, thereby reducing the number of classes in your public namespace list, and eliminating the need to have a unique name for each metadata class.

    using System.ComponentModel.DataAnnotations;
    
    namespace Validate.Models
    {
        [MetadataType(typeof(MetaData))]
        public partial class Person
        {
            public class MetaData
            {
                [Required]
                [Display(Name = "Enter Your Name")]
                public string FirstName;
    
                //...
            }
        }
    }
    

提交回复
热议问题