简体   繁体   English

如何使用VAB /实体框架验证单个属性?

[英]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.

相关问题 如何更新 Entity Framework Core 中的单个属性 - How to update a single property in Entity Framework Core 如何根据实体框架中的上下文验证实体? - How to validate entity against the context in Entity Framework? 当属性验证依赖于另一个属性值时,如何使用元数据验证实体框架 4 Model? - How do I Validate An Entity Framework 4 Model Using Metadata When The Property Validation Is Dependent on Another Property Value? 实体框架-选择按属性排序的单个记录 - Entity Framework - Select single record ordered by a property 如何在实体框架中包含实体特定的属性? - How to include a entity specific property in Entity Framework? 使用实体框架更新实体的单个属性时出错 - Error in updating single property of an entity using Entity Framework 验证VAB配置文件中的程序集和名称空间 - Validate assemblies and namespaces in VAB config file 如何在实体框架中列出连接的实体属性而不是完整实体? - How to list joined entity property instead of full entity in Entity Framework? 实体框架代码首先更新复杂类型的单个属性 - Entity Framework Code First Update a Single Property of a Complex Type 如何通过使用实体框架获得单一价值 - how to get single value by using entity framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM