The requested page is not available for the requested platform. You are viewing the content for Default platform.

Implement Dependent Reference Properties (EF)

  • 4 min to read

In this lesson, you will learn how to implement properties whose values can depend on other properties. The Manager property will be added to the Contact class. By default, it will be represented by a lookup editor containing all Contacts that exist in the database. However, you may need this editor to contain Contacts from the same Department. In addition, you may need the Position of these Contacts to be "Manager". To do this, use the DataSourcePropertyAttribute and DataSourceCriteriaAttribute attributes for the Manager property.

  • Add a new Manager property of the Contact type to the Contact class. Apply the DataSourceProperty attribute to this property, as shown below.

    using DevExpress.Persistent.Base;
    //...
    public class Contact : Person {
        public Contact() {
            //...
            Subordinates = new List<Contact>();
        }
        //...
        [DataSourceProperty("Department.Contacts")]
        public virtual Contact Manager { get; set; }
        public virtual IList<Contact> Subordinates { get; set; }
    }
    
    Note

    In EF, you always need to implement both "sides" of the relation. This means that each Contact must have a Manager and a list of Subordinates.

    With the DataSourceProperty attribute applied, the Manager lookup editor will contain Contact objects that are specified by the Department object's Contacts property.

  • Run the application and select Contact in the drop-down list of the New combo box. The Contact Detail View will be invoked. Specify the Department property and expand the Manager lookup editor. Make sure that the Department property of the listed objects is the same as those you specified above.

    Tutorial_BMD_Lesson7_1

  • Apply the DataSourceCriteria attribute to the Contact class' Manager property as shown below.

    public class Contact : Person {
        //...
        [DataSourceProperty("Department.Contacts")]
        [DataSourceCriteria("Position.Title = 'Manager'")]
        public virtual Contact Manager { get; set; }
    }
    

    With the DataSourceCriteria attribute applied, the Manager lookup editor will contain Contact objects that satisfy the criteria specified in the attribute parameter.

  • Run the application. Set the Position property to "Manager" for several Contact objects.

    Managers_XAF

  • Select Contact in the New (new_dropdown_btn) button's drop-down list. The Contact Detail View will be invoked. Specify the Department property and expand the Manager lookup editor. Check to make sure that the Position property is set to "Manager" for each of the listed objects.

    Tutorial_BMD_Lesson7_2

  • If the Department property is not specified for a Contact, you can provide another data source for the Manager lookup editor. To do this, specify the second parameter for the DataSourceProperty attribute. In the code below, this parameter is set to the DataSourcePropertyIsNullMode.SelectAll value. You can also set the DataSourcePropertyIsNullMode.SelectNothing or DataSourcePropertyIsNullMode.CustomCriteria values. In the latter case, a third parameter is required to specify a criterion.

    public class Contact : Person {
        //...
        [DataSourceProperty("Department.Contacts", DataSourcePropertyIsNullMode.SelectAll)]
        [DataSourceCriteria("Position.Title = 'Manager'")]
        public virtual Contact Manager { get; set; }
    }
    

    The code above will show all contacts in the Manager lookup editor if the Department property is not specified.

  • Run the application and check the results.

You can see the code demonstrated in this lesson in the MySolution.Module | Data | Contact.cs (Contact.vb) file of the EF Demo (Code First) installed with XAF. By default, the EF Demo (Code First) application is installed in %PUBLIC%\Documents\DevExpress Demos 19.1\Components\eXpressApp Framework\EFDemoCodeFirst.

 

Next Lesson: Implement Property Value Validation in Code (EF)