0

我在我的项目中使用 Wpf 和 EntityFrameWork。我想使用 DataAnnotations,因为它是验证数据的更好方法。我设法在我的实体文件中做到了。例如,我的实体之一包括:

public partial class Employee : INotifyPropertyChanged, IDataErrorInfo
{
    public Employee()
    {
        this.Address = new HashSet<Address>();
        this.Certificate = new HashSet<Certificate>();
        this.ContactInformation = new HashSet<ContactInformation>();
        this.Course = new HashSet<Course>();
        this.Education = new HashSet<Education>();
        this.EmployeeTeam = new HashSet<EmployeeTeam>();
        this.Evaluation = new HashSet<Evaluation>();
        this.ExamInformation = new HashSet<ExamInformation>();
        this.JobTitle = new HashSet<JobTitle>();
        this.MilitaryService = new HashSet<MilitaryService>();
        this.Relationship = new HashSet<Relationship>();
        this.WorkExperience = new HashSet<WorkExperience>();
    }

    public int Id { get; set; }


    private string _CitizenId;

    [Required(ErrorMessage = "CitizenId is required!")]
    [RegularExpression(@"^\d+$", ErrorMessage = "Only numbers allowed")]
    public string CitizenId
    {
        get
        {
            return _CitizenId;
        }

        set
        {
            _CitizenId = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("CitizenId"));
            }
        }
    }


    private int _RegNumber;
    [Required (ErrorMessage = "Registration Number is required!")]
    [RegularExpression(@"^\d+$", ErrorMessage = "Only numbers allowed")]

    public int RegNumber
    {
        get
        {
            return _RegNumber;
        }

        set
        {
            _RegNumber = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("RegNumber"));
            }
        }
    }
    public Nullable<short> ResourceTypeId { get; set; }
    public Nullable<short> EmployeeRoleId { get; set; }


    private string _FirstName;

    [Required(ErrorMessage="Name is required!")]
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage="Numeric expressions are not allowed!")]



    public string FirstName
    {
        get
        {
            return _FirstName;
        }

        set
        {
            _FirstName = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
            }
        }
    }

    private string _MiddleName;
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numeric expressions are not allowed!")]

    public string MiddleName
    {
        get
        {
            return _MiddleName;
        }

        set
        {
            _MiddleName = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("MiddleName"));
            }
        }
    }


    private string _Surname;
    [Required(ErrorMessage= "Surname is required!")]
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numeric expressions are not allowed!")]



    public string Surname {

        get
        {
            return _Surname;
        }

        set
        {
            _Surname = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Surname"));
            }
        }
    }


    private int _CustomerNo;
    [Required(ErrorMessage = "Customer Number is required!")]
    [RegularExpression(@"^\d+$", ErrorMessage = "Only numbers allowed")]
    public int CustomerNo
    {
        get
        {
            return _CustomerNo;
        }

        set
        {
            _CustomerNo = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("CustomerNo"));
            }
        }
    }


    public System.DateTime BirthDate { get; set; }



    private string _BirthPlace;
    [Required(ErrorMessage="Birh Place is required!")]
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numeric expressions are not allowed!")]

    public string BirthPlace
    {
        get
        {
            return _BirthPlace;
        }

        set
        {
            _BirthPlace = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("BirthPlace"));
            }
        }
    }
    public string Gender { get; set; }
    public string MaritalStatus { get; set; }
    public System.DateTime WorkBeginDate { get; set; }

    //indicates that whether the start  date is bigger than the end date which is a validation problem
  //  [CustomValidationsForDate("WorkBeginDate", ErrorMessage="Starting date cannot be bigger than the ending date")]
    public Nullable<System.DateTime> WorkEndDate { get; set; }

    private string _LogonName;
    [Required(ErrorMessage = "Logon Name is required!")]

    public string LogonName
    {
        get
        {
            return _LogonName;
        }

        set
        {
            _LogonName = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("LogonName"));
            }
        }
    }

    private int _DistanceToWork;
    [RegularExpression(@"^\d+$", ErrorMessage = "Only numbers allowed")]
    [Required(ErrorMessage="This field is required!")]
    public int DistanceToWork
    {
        get
        {
            return _DistanceToWork;
        }

        set
        {
            _DistanceToWork = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("DistanceToWork"));
            }
        }
    }
    public Nullable<bool> HasInternet { get; set; }
    public Nullable<bool> HasRemoteConnection { get; set; }
    public int MilitaryStatus { get; set; }
    public Nullable<int> DrivingLicense { get; set; }
    public bool IsSmoker { get; set; }


    public bool IsActive
    { get; set; }
    public Nullable<System.DateTime> CreateDate { get; set; }
    public string LeavingReason { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }




    public Nullable<System.DateTime> EndDate { get; set; }

    public virtual ICollection<Address> Address { get; set; }
    public virtual ICollection<Certificate> Certificate { get; set; }
    public virtual ICollection<ContactInformation> ContactInformation { get; set; }
    public virtual ICollection<Course> Course { get; set; }
    public virtual ICollection<Education> Education { get; set; }
    public virtual ICollection<EmployeeTeam> EmployeeTeam { get; set; }
    public virtual ICollection<Evaluation> Evaluation { get; set; }
    public virtual ICollection<ExamInformation> ExamInformation { get; set; }
    public virtual ICollection<JobTitle> JobTitle { get; set; }
    public virtual ICollection<MilitaryService> MilitaryService { get; set; }
    public virtual ICollection<Relationship> Relationship { get; set; }
    public virtual ICollection<WorkExperience> WorkExperience { get; set; }


    //Following methos have been used in order to apply DataAnnotations


    public event PropertyChangedEventHandler PropertyChanged;

    public string Error { get { return String.Empty; } }

    public string this[string property]
    {
        get { return ValidateProperty(this, property); }
    }

    public static string ValidateProperty(object instance, string propertyName)
    {
        PropertyInfo property = instance.GetType().GetProperty(propertyName);
        object value = property.GetValue(instance, null);
        List<string> errors = (from v in property.GetCustomAttributes(true).OfType<ValidationAttribute>()
                               where !v.IsValid(value)
                               select v.ErrorMessage).ToList();
        return (errors.Count > 0) ? String.Join("\r\n", errors) : null;
    }


}
}

所以就像我说的那样,我在实体内部应用数据注释,但我想要在外部执行此操作,因为每当我的数据库发生更改时,所有实体都会被刷新,所以我在这里拥有的不是动态结构。那你能帮忙吗?

提前致谢。此致。

埃格

4

1 回答 1

0

您应该创建一个模型(视图模型)类并从您的实体(域模型)映射到视图模型。AutoMapper 可以帮助加快这一进程。然后将注释放在视图模型上并在视图中使用它们。

这意味着如果您的实体模型发生更改,您只需要更新映射。这也意味着您可以为同一实体创建多个不同的模型,因此您的验证不必对使用它的每个视图都相同。

于 2012-07-19T09:42:54.590 回答