none
data annotations in asp.net mvc3 using c# database first RRS feed

  • Question

  • i have a class Customer.

    public class Customer

    {

    public int ID {get;set;}

    public string Name{get;set;}

    public string Email{get;set;}

    }

    if i create two properties of it i.e.

    public class CustomerInformation

    {

    public Customer current {get;set;}

    public Customer previous {get;set;}

    }

    while doing the data annotation validation when i pass Customer class it does the validation for both current and previous objects whereas i want the validation to be done only on current object as previous is my read-only data. how can i achieve that??

     [MetadataType(typeof(CustomerAnnotations))]
        public partial class Customer
        {
        }

        public class CustomerAnnotations
        {

              [DataType(DataType.Email, ErrorMessage = "Enter a valid email address")]
              [Required]
              public virtual string Email{ get; set; }

        }

    Friday, April 19, 2013 10:02 AM

Answers

  • Hi sumna,

    If you have created the metadata class, EF will apply the validation rules to all the Customer classes.

    What is CustomerInformation class in your scenario? If that is also an entity, you may try to combine metadata class and custom validation.

    [MetadataType(typeof(CustomerInformationMetadata))]
    public partial class CustomerInformation {}
    
    public class CustomerInformationMetadata
    {
        [CustomValidation(typeof(CurrentValidation), "Validate")]
        public Customer current { get; set; }
    }
    
    public static class CurrentValidation
    {
        public static ValidationResult Validate(Customer c)
        {
            if (string.IsNullOrEmpty(c.Email))
            {
                return new ValidationResult("Enter a valid email address");
            }
            return ValidationResult.Success;
        }
    }
    

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 23, 2013 7:45 AM
    Moderator

All replies

  • i have a class Customer.

    public class Customer

    {

    public int ID {get;set;}

    public string Name{get;set;}

    public string Email{get;set;}

    }

    if i create two properties of it i.e.

    public class CustomerInformation

    {

    public Customer current {get;set;}

    public Customer previous {get;set;}

    }

    while doing the data annotation validation when i pass Customer class it does the validation for both current and previous objects whereas i want the validation to be done only on current object as previous is my read-only data. how can i achieve that??

     [MetadataType(typeof(CustomerAnnotations))]
        public partial class Customer
        {
        }

        public class CustomerAnnotations
        {

              [DataType(DataType.Email, ErrorMessage = "Enter a valid email address")]
              [Required]
              public virtual string Email{ get; set; }

        }

    Friday, April 19, 2013 10:17 AM
  • Hi sumna,

    If you have created the metadata class, EF will apply the validation rules to all the Customer classes.

    What is CustomerInformation class in your scenario? If that is also an entity, you may try to combine metadata class and custom validation.

    [MetadataType(typeof(CustomerInformationMetadata))]
    public partial class CustomerInformation {}
    
    public class CustomerInformationMetadata
    {
        [CustomValidation(typeof(CurrentValidation), "Validate")]
        public Customer current { get; set; }
    }
    
    public static class CurrentValidation
    {
        public static ValidationResult Validate(Customer c)
        {
            if (string.IsNullOrEmpty(c.Email))
            {
                return new ValidationResult("Enter a valid email address");
            }
            return ValidationResult.Success;
        }
    }
    

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 23, 2013 7:45 AM
    Moderator