expressappframework-devexpress-dot-persistent-dot-validation-633ae9c3.md
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.v25.2.dll
NuGet Package : DevExpress.Persistent.Base
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Interface, AllowMultiple = true)]
public class RuleIsReferencedAttribute :
RuleBaseAttribute,
IRuleIsReferencedProperties,
IRuleSearchObjectProperties,
IRuleCollectionPropertyProperties,
IRuleBaseProperties
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Property Or AttributeTargets.Interface, AllowMultiple:=True)>
Public Class RuleIsReferencedAttribute
Inherits RuleBaseAttribute
Implements IRuleIsReferencedProperties,
IRuleSearchObjectProperties,
IRuleCollectionPropertyProperties,
IRuleBaseProperties
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:
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;
// ...
[RuleIsReferenced("",DefaultContexts.Delete,typeof(Contact),"Department",InvertResult=true,
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.
using DevExpress.Persistent.Validation;
// ...
[RuleIsReferenced("",DefaultContexts.Delete,typeof(Contact),"Department",InvertResult=true,
CriteriaEvaluationBehavior = CriteriaEvaluationBehavior.BeforeTransaction,
MessageTemplateMustBeReferenced = "The {TargetObject} object must not be referenced.")]
public class Department : BaseObject {
//...
}
public class Contact : BaseObject {
public Department Department {
//...
}
//...
}
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.
Tip
You can find examples of this functionality in the MainDemo demo included with XAF. The demo illustrates various XAF features (including RuleIsReferenced and other validation attributes) and is located in the %PUBLIC%\Documents\DevExpress Demos 25.2\Components\XAF\MainDemo.NET.EFCore folder.
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 RuleIsReferencedAttribute
See Also