EntityFramework-DBFirst-重新生成后写的验证消失(解决办法)
1、这是EF生成的类:
namespace PMS.DataProvider.EntityFramework { using System; using System.Collections.Generic; public partial class M_Managers { public string UserName { get; set; } public string NickName { get; set; } public string PassWord { get; set; } public string HeadImg { get; set; } public int Age { get; set; } public bool IsMale { get; set; } public string Email { get; set; } public string Mobile { get; set; } public string Address { get; set; } public string Signature { get; set; } public string Roles { get; set; } public string Description { get; set; } public System.DateTime CreateDate { get; set; } public string CreateUser { get; set; } public System.DateTime LastLoginDate { get; set; } public int LoginTimes { get; set; } } }
2、新建一个部分(partial)类:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PMS.DataProvider.EntityFramework { [MetadataType(typeof(M_ManagersMetadata))] public partial class M_Managers { } public class M_ManagersMetadata { [Display(Name = "用户名")] [Required(ErrorMessage = "*")] [StringLength(50, ErrorMessage = "字符长度应在6-50之间", MinimumLength = 6)] public string UserName { get; set; } [Display(Name = "显示名")] [Required(ErrorMessage = "*")] [StringLength(50, ErrorMessage = "字符长度应在6-50之间", MinimumLength = 6)] public string NickName { get; set; } [Display(Name = "密码")] [Required(ErrorMessage = "*")] [DataType(DataType.Password)] [StringLength(50, ErrorMessage = "字符长度应在6-50之间", MinimumLength = 6)] public string PassWord { get; set; } [Display(Name = "头像")] [DataType(DataType.ImageUrl)] public string HeadImg { get; set; } [Display(Name = "年龄")] [Range(10, 120, ErrorMessage = "年龄应在10-120之间")] public int Age { get; set; } [Display(Name = "性别")] public bool IsMale { get; set; } [Display(Name = "邮箱")] [Required(ErrorMessage = "*")] [DataType(DataType.EmailAddress)] [EmailAddress(ErrorMessage = "邮箱格式不正确")] [StringLength(50, ErrorMessage = "字符长度应在6-50之间", MinimumLength = 6)] public string Email { get; set; } [Display(Name = "手机")] [Required(ErrorMessage = "*")] public string Mobile { get; set; } [Display(Name = "地址")] public string Address { get; set; } [Display(Name = "签名")] public string Signature { get; set; } [Display(Name = "角色")] public string Roles { get; set; } [Display(Name = "描述")] [DataType(DataType.MultilineText)] public string Description { get; set; } [Display(Name = "创建日期")] [DataType(DataType.DateTime, ErrorMessage = "日期格式不正确")] public System.DateTime CreateDate { get; set; } [Display(Name = "创建人")] public string CreateUser { get; set; } [Display(Name = "最后登录日期")] [DataType(DataType.Date, ErrorMessage = "日期格式不正确")] public System.DateTime LastLoginDate { get; set; } [Display(Name = "登录次数")] public int LoginTimes { get; set; } } }
注意:
[MetadataType(typeof(M_ManagersMetadata))]//给新建的部分类指定要关联的元数据类 public partial class M_Managers//这是新建的部分类(里面是空的,啥也不写),注意要与EF生成的类一样,包括类名称和命名空间 { } public class M_ManagersMetadata//新建一个元数据类 { //这里写Model验证 [Display(Name = "用户名")] [Required(ErrorMessage = "*")] [StringLength(50, ErrorMessage = "字符长度应在6-50之间", MinimumLength = 6)] public string UserName { get; set; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。