我正在使用Entity Framework,所有实体都继承自BaseObject:
public class BaseObject : IDataErrorInfo
{
private string _validationMessage;
public BaseObject()
{
_validationMessage = string.Empty;
}
public void Validate()
{
Validator validator = ValidationFactory.CreateValidator(GetType());
var validationResults = validator.Validate(this);
if (validationResults.Count > 0)
{
StringBuilder message = new StringBuilder();
foreach (var validationResult in validationResults)
{
message.Append(validationResult.Message);
message.Append(Environment.NewLine);
}
_validationMessage = message.ToString();
//throw new ValidationException(message.ToString());
}
}
public string Error
{
get
{
_validationMessage = string.Empty;
this.Validate();
return _validationMessage;
}
}
public string this[string columnName]
{
get
{
_validationMessage = string.Empty;
this.Validate();
return _validationMessage;
}
}
}
BaseObjects实现了IDataErrorInfo接口,因此我可以将ErrorProvider与bindingsource结合使用。此代码的问题是,当一个属性无效时,所有其他属性也是无效的。所以我的问题是,我该如何解决这个问题呢?我正在使用验证应用程序块,我不知道如何验证单个属性。