Back to Devexpress

Implement Dependent Reference Properties (EF 6)

expressappframework-113637-getting-started-in-depth-tutorial-winforms-webforms-business-model-design-business-model-design-with-entity-framework-6-implement-dependent-reference-properties-ef.md

latest3.9 KB
Original Source

Implement Dependent Reference Properties (EF 6)

  • Jul 07, 2021
  • 4 minutes 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 Contact s that exist in the database. However, you may need this editor to contain Contact s from the same Department. In addition, you may need the Position of these Contact s to be “Manager”. To do this, use the DataSourcePropertyAttribute and DataSourceCriteriaAttribute attributes for the Manager property.

Note

Before proceeding, take a moment to review the following lessons:

  • Inherit from the Business Class Library Class (EF 6)

  • Implement Custom Business Classes and Reference Properties (EF 6)

  • Set a One-to-Many Relationship (EF 6)

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

  • 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.

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

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

  • Select Contact in the New ( ) 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.

  • 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.

  • 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 21.2\Components\eXpressApp Framework\EFDemoCodeFirst.

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