Skip to main content
.NET 6.0+

RuleObjectExistsAttribute Class

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.v23.2.dll


[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Interface, AllowMultiple = true)]
public class RuleObjectExistsAttribute :


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:

  • Specify the criterion via the criteria parameter. In addition to common rules of writing criteria that are described in the Ways to Build Criteria topic, there is one more capability. To get the current value of the validated object’s property, use the following construction: 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.
  • Specify a class name via the RuleObjectExistsAttribute.LooksFor parameter, to look for objects of the type that differ from the target’s type.
  • Specify whether to look for objects that are currently retrieved from the database, in addition to the objects in the database itself. To do this, use the RuleObjectExistsAttribute.CriteriaEvaluationBehavior parameter.
  • Specify whether to check the current business object by this rule. Use the RuleObjectExistsAttribute.IncludeCurrentObject parameter.
  • Invert the rule so that it is broken when there is at least one object of the specified type that satisfies the criterion. Use the RuleBaseAttribute.InvertResult parameter. Do not forget to specify an inverted message template, because the default one is not oriented on the inverted result. To do this, use the CustomMessageTemplate parameter. However, the list of the found objects will not be written in this instance. So, you can use the RuleObjectExistsAttribute.MessageTemplateMustExist parameter instead.

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.

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 23.2\Components\XAF\FeatureCenter.NETFramework.XPO folder, by default.


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.


See Also