locked
Can Entity Framework 6.1 infer dataannotations automatically to perform mvc 5 input controls validation? RRS feed

  • Question

  • User1529754709 posted

    Can Entity Framework 6.1 infer dataannotations automatically to perform mvc 5 input controls validation?

    Is there an option for this other than T4?

    Thanks in advance.

    Saturday, June 20, 2015 8:00 AM

Answers

  • User-84896714 posted

    Hi varkz,

    Thank you for your post.

    According to your description, you could change T4 templates to generate validate dataannotations for entities.

    Link below contains a example.

    http://blogs.msdn.com/b/lgmorand/archive/2010/12/31/entity-framework-template-t4-and-data-annotations.aspx

    In addition, you could add MetadataType attribute to entity framework for alternative solution.

    [MetadataType(typeof(Student_Validate))]
    public partial class Student
    {
    }
    public class Student_Validate
    {
        [StringLength(50, ErrorMessage = "Must be under 50")]
        [Required(ErrorMessage = "Name is request")]
        public string Name { get; set; }
    }

    Hoping my reply could be helpful to you.
    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 23, 2015 4:00 AM

All replies

  • Saturday, June 20, 2015 8:23 AM
  • User1529754709 posted

    Thak you, Mostafa for your reply. i see that the automatic validation occurs when saving the entity. i was looking for something like validation attributes decorating properties of a class so that the validation could be used with the input at the browser. Since the metadata exists and for example mandatory fields and field max sizes etc... Is there such an option? Maybe i have to go for T4 templates, right?

    Thank you,

    Saturday, June 20, 2015 9:25 AM
  • User-84896714 posted

    Hi varkz,

    Thank you for your post.

    According to your description, you could change T4 templates to generate validate dataannotations for entities.

    Link below contains a example.

    http://blogs.msdn.com/b/lgmorand/archive/2010/12/31/entity-framework-template-t4-and-data-annotations.aspx

    In addition, you could add MetadataType attribute to entity framework for alternative solution.

    [MetadataType(typeof(Student_Validate))]
    public partial class Student
    {
    }
    public class Student_Validate
    {
        [StringLength(50, ErrorMessage = "Must be under 50")]
        [Required(ErrorMessage = "Name is request")]
        public string Name { get; set; }
    }

    Hoping my reply could be helpful to you.
    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 23, 2015 4:00 AM
  • User1529754709 posted

    Hi Li,

    Thank you for the T4 example, maybe the best solution is to make a template for this. I think this feature should be an option for scaffolding, and taken a little further having naming conventions for fields like credit card or phone or e-mail etc, ( like it is already done with controllers ), so that some dataannotations can be applied automatically. i think MVC is a great plattform, it allows for greater control over rendered html an cleaner code than web forms ( that i used for some years ). Web forms is still much faster to get things done but the ease of Dependency Injection and Automated testing for MVC are important. Maybe with the addition of these kind of features to MVC plattform, the development can be speeded up in cases of EF database first or code first with existing database with several entities and large collections of collumns.

    Thank you all for participating.

    Tuesday, June 23, 2015 10:45 PM