엔티티 프레임 워크를 사용하고 유효성 검사에 데이터 주석을 사용하려고합니다. 나는 구글에서 몇 가지 예를 찾아 보았고 같은 구조를 도처에 발견했다. 나는 그것을 따라 갔지만, 어떤 이유로 나는 나의 오류가 그 형태로 나타나지 않는다. 나는 Validator
클래스를 사용하여 프로퍼티를 수동으로 검증해야 할지도 모르지만 어디서 처리해야하는지 알 수 없다. 나는 PropertyChanging
이벤트를들을 수 있지만 할당하려고하는 값이 아니라 속성의 이름 만 전달한다는 것을 알고 있습니다. 누구든지 내가이 문제를 어떻게 해결할 수 있을지 생각해?EntityFramework 및 DataAnnotations 오류가 표시되지 않습니다.
미리 감사드립니다.
[MetadataType(typeof(Employee.MetaData))]
public partial class Employee
{
private sealed class MetaData
{
[Required(ErrorMessage = "A name must be defined for the employee.")]
[StringLength(50, ErrorMessage="The name must be less than 50 characters long.")]
public string Name { get; set; }
[Required(ErrorMessage="A username must be defined for the employee.")]
[StringLength(20, MinimumLength=3, ErrorMessage="The username must be between 3-20 characters long.")]
public string Username { get; set; }
[Required(ErrorMessage = "A password must be defined for the employee.")]
[StringLength(20, MinimumLength = 3, ErrorMessage = "The password must be between 3-20 characters long.")]
public string Password { get; set; }
}
}
<fx:TextBox Width="250" Height="20" CornerRadius="5" BorderThickness="0" MaxLength="50" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
<fx:TextBox Width="250" Height="20" CornerRadius="5" BorderThickness="0" MaxLength="20" Text="{Binding Username, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" />
<fx:PasswordBox Width="250" Height="20" CornerRadius="5" BorderThickness="0" MaxLength="20" Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
가 편집 XAML :
public static class EntityHelper
{
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;
}
}
[MetadataType(typeof(Employee.MetaData))]
public partial class Employee:IDataErrorInfo
{
private sealed class MetaData
{
[Required(ErrorMessage = "A name must be defined for the employee.")]
[StringLength(50, ErrorMessage="The name must be less than 50 characters long.")]
public string Name { get; set; }
[Required(ErrorMessage="A username must be defined for the employee.")]
[StringLength(20, MinimumLength=3, ErrorMessage="The username must be between 3-20 characters long.")]
public string Username { get; set; }
[Required(ErrorMessage = "A password must be defined for the employee.")]
[StringLength(20, MinimumLength = 3, ErrorMessage = "The password must be between 3-20 characters long.")]
public string Password { get; set; }
}
public string Error { get { return String.Empty; } }
public string this[string property]
{
get { return EntityHelper.ValidateProperty(this, property); }
}
XAML (레이첼의 의견에 따라 IDataErrorInfo 클래스를 구현)
<fx:TextBox Width="250" Height="20" CornerRadius="5" BorderThickness="0" MaxLength="50" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
이런 결혼! 이 방법을 계속할 때 유지 보수의 악몽으로 나아갈 것이므로 유효성 검사 논리를 구현하기 위해 다른 접근법을 사용하는 것이 좋습니다. 왜 DataAnnotations 또는 Validation Application Block을 사용하고 DataErrorInfoBase 클래스와 통합할까요? 그러한 통합에 대한 예를 살펴보십시오 (http://bit.ly/crLXAz). – Steven
@Steven EntityFramework를 사용하고 있으므로 생성 된 엔티티는 EntityObject에서 파생되므로 다른 기본 클래스를 추가 할 수 없습니다. 다른 어떤 링크가 도움이 될 것이라고 생각하십니까? 감사. –
내 [업데이트] (http://bit.ly/crLXAz)를 참조하십시오. EF 3.5에서이 작업을 수행하는 방법을 설명합니다. – Steven