MVC4 + EF Adds a separate validation attribute for the model

Source: Internet
Author: User

You can use the following methods to add related meta-validation attributes to the model so that the entity's validation properties are not replaced by the model, such as EF or other tools, that are automatically generated.

Using System.ComponentModel.DataAnnotations;

Namespace IDO. Entities
{
[Metadatatype (typeof (Customermetadata))]
Partial class Customer
{
}

public class Customermetadata
{
[Required (errormessage = "{0} cannot be empty")]
public string Name {get; set;}

[Required (errormessage = "{0} cannot be empty")]
[Stringlength (one, errormessage = "{0} cannot exceed {1} bits")]
[RegularExpression (@ "^1[3458]\d{9}$", errormessage = "{0} Invalid format")]
public string Mobile {get; set;}

[RegularExpression (@ "^ [\w-\.] +) @ ((\[[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\.) | ([\w-]+\.) +)) ([a-za-z]{2,4}| [0-9] {1,3}) (\]?) $ ", errormessage =" {0} format is invalid ")]
public string e-mail {set; get;}

[Required (errormessage = "{0} cannot be empty")]
public string Title {get; set;}
}
}

MVC4 + EF Adds a separate validation attribute for the model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.