[英]How to validate a single property with VAB / Entity framework?
I'm using Entity Framework and all the entities inherits from BaseObject: 我正在使用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 implements the IDataErrorInfo interface so I can use the ErrorProvider in combination with a bindingsource. BaseObjects实现IDataErrorInfo接口,因此我可以将ErrorProvider与绑定源结合使用。 The problem with this code is that when one property is invalid, all the other properties are invalid too.
此代码的问题在于,当一个属性无效时,所有其他属性也无效。 So my question is, how can I solve this?
所以我的问题是,我该如何解决呢? I am using the Validation Application Block and I don't know how I can validate a single property.
我正在使用验证应用程序块,我不知道如何验证单个属性。
@Tuzo:我认为可以通过使用PropertyValidationFactory.GetPropertyValidator方法来实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.