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
Position
class. 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
Position
type:public class MySolutionEFCoreDbContext : DbContext { //... public DbSet<Position> Positions { get; set; } }
Add the
Position
property to theEmployee
class://... using System.Collections.ObjectModel; namespace MySolution.Module.BusinessObjects; [DefaultClassOptions] public class Employee : BaseObject { //... public virtual Position Position { get; set; } }
The
Employee
class now exposes thePosition
reference property. This way, you effectively create a “One-to-Many” relationship between these entity classes.Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.
Run the application.
You can see that the navigation control displays the Position item. Click this item to access the
Position
List View. Click New to open thePosition
Detail View.Create Developer and Manager positions. They now appear in the
Position
List View:- ASP.NET Core Blazor
- Windows Forms
Open the Employee Detail View. In this view, XAF creates a lookup editor for the
Position
reference property.- ASP.NET Core Blazor
- Windows Forms
Exercise: Add an “Address” Reference Property
Use the same steps to add an
Address
reference property to theEmployee
entity. You can find the type declaration in the code sample below.Tip
Remember to register the new entity in
DbContext
, add a new property to theEmployee
class, and create a new migration for the database.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
Address
field:- ASP.NET Core Blazor
- Windows Forms