Implement Dependent Reference Properties (EF Core)
- 2 minutes to read
This lesson explains how to implement properties whose values can depend on other properties.
You will add a new Manager property to the Contact class. The editor for this property will display a list of managers who work in the same department.
Note
Before you proceed, take a moment to review the following lessons:
Step-by-Step Instructions
Add the new Manager and Subordinates properties to the Contact class:
using DevExpress.Persistent.Base; //... public class Contact : Person { public Contact() { //... Subordinates = new List<Contact>(); } //... Contact manager; public virtual Contact Manager { get => manager; set => SetReferencePropertyValue(ref manager, value); } IList<Contact> subordinates; public virtual IList<Contact> Subordinates { get => subordinates; set => SetReferencePropertyValue(ref subordinates, value); } }
Apply the DataSourceProperty and DataSourceCriteria attributes to the newly added property.
[DefaultClassOptions] public class Contact : Person { //... [DataSourceProperty("Department.Contacts", DataSourcePropertyIsNullMode.SelectAll)] [DataSourceCriteria("Position.Title = 'Manager'")] public virtual Contact Manager { // ... } // ... }
Run the application.
Add the following objects:
- A Department object (for example, “Developer Department”).
- Multiple Contact objects with the Department property set to “Developer Department”.
- Multiple Position objects (for example, “Manager”, “Developer”).
For the newly added contacts, set the Position property to:
- “Manager” (for multiple Contact objects).
- “Developer” (for other Contact objects).
Create a new Contact object. In the Contact Detail View, specify the Department property and expand the Manager lookup editor. Notice the following:
- The Department property of the listed objects is the same as those you specified above.
- The Position property is set to “Manager” for each of the listed objects.
Detailed Explanation
The DataSourceProperty Attribute
The DataSourceProperty attribute accepts two parameters. The first one specifies the path to the lookup list. The second parameter is optional. It specifies how the lookup items are populated if the application could not find any items from the path.
In this tutorial, the second parameter is set to SelectAll. You can also set it to SelectNothing or CustomCriteria. In the latter case, a third parameter is required to specify criteria.
The DataSourceCriteria Attribute
With the DataSourceCriteria attribute applied, the Manager lookup editor contains Contact objects that satisfy the criteria specified in the attribute parameter.
Next Lesson
Implement Property Value Validation in Code (EF Core)