2012-07-19 30 views
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; 
    } 


} 
} 

所以,就像我說我申請dataannotations實體內,但我要的是這樣做之外,因爲每當一個變化在我的數據庫時,所有的實體將是刷新,所以我在這裏不是一個動態結構。 那麼你可以幫忙嗎?

在此先感謝。 此致敬禮。

愛琴

回答

0

您應該創建一個模型(視圖模型)類,並從實體(域模型)到視圖模型圖。 AutoMapper可以幫助加快速度。然後,將註釋放在視圖模型上,並在視圖中使用它們。

這意味着如果您的實體模型發生更改,您只需更新映射。這也意味着您可以爲同一個實體創建多個不同的模型,因此您的驗證對於使用它的每個視圖而言不必相同。