Attaching validation to EF objects used in MVC controllers/views?

前端 未结 3 1209
青春惊慌失措
青春惊慌失措 2021-01-21 04:26

We\'re throwing together a quick project (CRUD forms) and decided to skip view models and use EF entities directly in controllers and views. Since I\'m not used to this approach

3条回答
  •  渐次进展
    2021-01-21 04:38

    This can be done using MetadataType attribute on the Ef generated classes. The EF generates partial classes. So those can be extended and attribute added to it. Then another "buddy class" can be written that can have member decoration. For example

    [MetadataType(typeof(EFGeneratedClass_MetaData))]
    public partial class EFGeneratedClass
    {
    }
    
    public partial class EFGeneratedClass_MetaData
    {
        [Required]
        [Display(Name="Member1 Display")]
        public string Member1 {get; set;}
    }
    

提交回复
热议问题