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 string AdminTypeStr
        {
            get
            { if (AdminType == "SuperAdmin") { return "超级管理员"; } else { return "普通管理员"; } }
            set { value = AdminType; }
        }

}
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; }
   }

 

posted @ 2015-05-25 11:36  小-牛  阅读(417)  评论(0编辑  收藏  举报