Skip to main content
.NET 6.0+

RuleIsReferencedAttribute Class

Defines a validation rule demanding that an object should be referenced in objects of a specified type.

Namespace: DevExpress.Persistent.Validation

Assembly: DevExpress.Persistent.Base.v23.2.dll


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


Apply this attribute to a business class to define a validation rule that is broken when there are no objects of the specified type that have a reference to the current object of the target type. Use the attribute’s parameters for the following:

  • Specify the type of the objects that are tested to determine that they have a reference to the current object. Use the RuleIsReferencedAttribute.LooksFor parameter.
  • Specify the name of the reference property that can be set to the current object. Use the referencePropertyName parameter.
  • 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 RuleIsReferencedAttribute.CriteriaEvaluationBehavior attribute.
  • Invert the rule so that it is broken when there is at least one object of the specified type that references the current object. 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 RuleIsReferencedAttribute.MessageTemplateMustBeReferenced parameter instead.

In addition, use the common parameters that are inherited from the RuleBaseAttribute class.

The following example demonstrates how to define a rule that will be broken when deleting a Department object, if there is one or more Contact objects that refer this object by the “Department” property. The invoked error message will list the Contact objects that have a reference to the Department object.

using DevExpress.Persistent.Validation;
using System.ComponentModel.DataAnnotations;
// ...
   CriteriaEvaluationBehavior = CriteriaEvaluationBehavior.BeforeTransaction,
   MessageTemplateMustBeReferenced = "The {TargetObject} object must not be referenced.")]
public class Department : BaseObject {

public class Contact : BaseObject {
    public virtual Department Department { get; set; }

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.

The rule generated by the RuleIsReferencedAttribute 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 RuleIsReferenced 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