zoukankan      html  css  js  c++  java
  • MVC4 + EF为Model添加单独的验证属性

    可使用以下方式给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; }
     }
    }

  • 相关阅读:
    dup/dup2函数
    read/write函数
    lseek函数
    流程控制
    vim普通模式
    vim实用技巧1
    python源代码解读
    python变量命名规则
    python之字符串2
    Docker系列文章
  • 原文地址:https://www.cnblogs.com/itjeff/p/3941527.html
Copyright © 2011-2022 走看看