expressappframework-devexpress-dot-persistent-dot-validation-47a13896.md
Defines a validation rule that demands a particular type object that satisfies a specified criterion, exist in the database.
Namespace : DevExpress.Persistent.Validation
Assembly : DevExpress.Persistent.Base.v25.2.dll
NuGet Package : DevExpress.Persistent.Base
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Interface, AllowMultiple = true)]
public class RuleObjectExistsAttribute :
RuleBaseAttribute,
IRuleObjectExistsProperties,
IRuleSearchObjectProperties,
IRuleCollectionPropertyProperties,
IRuleBaseProperties
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Property Or AttributeTargets.Interface, AllowMultiple:=True)>
Public Class RuleObjectExistsAttribute
Inherits RuleBaseAttribute
Implements IRuleObjectExistsProperties,
IRuleSearchObjectProperties,
IRuleCollectionPropertyProperties,
IRuleBaseProperties
Apply this attribute to a business class to define a validation rule that will check whether there are target type objects that satisfy a specified criterion. Use the attribute’s parameters for the following:
PropertyName = '@PropertyName' (the operator is optional). @PropertyName represents a parameter that is set to the value of the PropertyName property of the currently validated object, when the rule is checked.In addition, use the common parameters that are inherited from the RuleBaseAttribute class.
[RuleObjectExists("",DefaultContexts.Save,"Office=205",InvertResult = true,
CriteriaEvaluationBehavior = CriteriaEvaluationBehavior.BeforeTransaction,
MessageTemplateMustExist = "The objects that satisfy the '{Criteria}' criteria must not exist")]
public class Department : BaseObject {
public virtual string Office { get; set; }
}
// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
[RuleObjectExists("",DefaultContexts.Save,"Office=205",InvertResult = true,
CriteriaEvaluationBehavior = CriteriaEvaluationBehavior.BeforeTransaction,
MessageTemplateMustExist = "The objects that satisfy the '{Criteria}' criteria must not exist")]
public class Department : BaseObject {
//...
public string Office {
//...
}
}
The rule generated by the RuleObjectExistsAttribute will be loaded to the Application Model‘s IModelValidationRules node. So, you can customize this rule via the Model Editor. In addition, you can create new rules in the Model Editor as well. This is helpful when you need to add a rule to a property which is implemented in a base class provided by a third party library. For details, refer to the Implement Property Value Validation topic.
You can see examples of using the RuleObjectExists and other validation attributes in the Validation section of the FeatureCenter demo. This demo is located in the %PUBLIC%\Documents\DevExpress Demos 25.2\Components\XAF\FeatureCenter.NET.XPO folder, by default.
Note
You can apply RuleUniqueValue, RuleObjectExists, RuleIsReferenced, and RuleCombinationOfPropertiesIsUnique rules to persistent business objects only because they search for objects within the application database. If the rule is applied to a base class that has descendants, the condition is tested against objects of all derived types.
IRuleCollectionPropertyProperties
Object Attribute RuleBaseAttribute RuleObjectExistsAttribute
See Also