可使用以下方式给Model加上相关的meta验证属性,这样实体的验证属性就不会被例如EF或其他工具自动生成的Model所替换了.
using System.ComponentModel.DataAnnotations;
namespace IDO.Entities
{ [MetadataType(typeof(CustomerMetadata))] partial class Customer { }public class CustomerMetadata
{ [Required(ErrorMessage = "{0}不能为空")] public string Name { get; set; }[Required(ErrorMessage = "{0}不能为空")]
[StringLength(11, ErrorMessage = "{0}不能超过{1}位")] [RegularExpression(@"^1[3458]\d{9}$", ErrorMessage = "{0}格式无效")] 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}格式无效")]
public string Email { set; get; }[Required(ErrorMessage = "{0}不能为空")]
public string Title { get; set; } } }