24 July 2013

Adding a Validator to An Existing Model Generated by Designer

You have a model (edmx) generated by the designer. You have a table named Animal. Now, you want to add a validator directly to your model. How can you do this?

Okay, here's a simple step to make it work:

First create a class that will hold the validation. For example,

public class MyValidator
        {
            [Required]
            public string Name { get; set; }

            [Required]
            public int Age { get; set; }

            [DataType(DataType.Date)]
            public DateTime? BirthDate { get; set; }
        }


Next, look inside the model and add a MetadataType on top of the entity object you want to validate.

[MetadataType(typeof(MyValidator))]

public partial class Animal : EntityObject


That's it. Period.

No comments:

Post a Comment