Implement Reference Properties
- 3 minutes to read
This lesson explains the following concepts:
- How to implement object references to existing classes.
- How XAF generates a UI for referenced classes.
Note
Before you proceed, take a moment to review the previous lesson:
Step-by-Step Instructions
In the MySolution.Module\Business Objects folder, create the
Positionclass. Replace the generated class declaration with the following code:using DevExpress.Persistent.Base; using DevExpress.Persistent.BaseImpl.EF; using System.ComponentModel; namespace MySolution.Module.BusinessObjects { [DefaultClassOptions] [DefaultProperty(nameof(Title))] public class Position : BaseObject { public virtual string Title { get; set; } } }Go to the MySolution.Module\MySolutionDbContext file and add a DbSet of
Positiontype:public class MySolutionEFCoreDbContext : DbContext { //... public DbSet<Position> Positions { get; set; } }Add the
Positionproperty to theEmployeeclass://... using System.Collections.ObjectModel; namespace MySolution.Module.BusinessObjects; [DefaultClassOptions] public class Employee : BaseObject { //... public virtual Position Position { get; set; } }The
Employeeclass now exposes thePositionreference property. This way, you effectively create a “One-to-Many” relationship between these entity classes.Run the application.
You can see that the navigation control displays the Position item. Click this item to access the
PositionList View. Click New to open thePositionDetail View.Create Developer and Manager positions. They now appear in the
PositionList View:- ASP.NET Core Blazor

- Windows Forms

Open the Employee Detail View. In this view, XAF creates a lookup editor for the
Positionreference property.- ASP.NET Core Blazor

- Windows Forms

Exercise: Add an “Address” Reference Property
Use the same steps to add an
Addressreference property to theEmployeeentity. You can find the type declaration in the code sample below.Tip
Remember to register the new entity in
DbContextand add a new property to theEmployeeclass.using DevExpress.Persistent.Base; using DevExpress.Persistent.BaseImpl.EF; using System.ComponentModel; namespace MySolution.Module.BusinessObjects; [DefaultProperty(nameof(FullAddress))] public class Address : BaseObject { private const string defaultFullAddressFormat = "{Country}; {StateProvince}; {City}; {Street}; {ZipPostal}"; public virtual String Street { get; set; } public virtual String City { get; set; } public virtual String StateProvince { get; set; } public virtual String ZipPostal { get; set; } public virtual String Country { get; set; } public String FullAddress { get { return ObjectFormatter.Format(defaultFullAddressFormat, this, EmptyEntriesMode.RemoveDelimiterWhenEntryIsEmpty); } } }This class is not visible in the navigation control, but you can create objects of this type from the reference property lookup editor.
Run the application and open the Employee Detail View. It should now contain the
Addressfield:- ASP.NET Core Blazor

- Windows Forms
