Skip to main content
All docs
V24.2
.NET 8.0+

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

  1. 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; }
        }
    }
    
  2. Go to the MySolution.Module\MySolutionDbContext file and add a DbSet of Position type:

    public class MySolutionEFCoreDbContext : DbContext {
        //...
        public DbSet<Position> Positions { get; set; } 
    }
    
  3. Add the Position property to the Employee 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 the Position reference property. This way, you effectively create a “One-to-Many” relationship between these entity classes.

  4. Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.

  5. 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 the Position Detail View.

  6. Create Developer and Manager positions. They now appear in the Position List View:

    ASP.NET Core Blazor
    ASP.NET Core Blazor list view
    Windows Forms
    Windows Forms list view
  7. Open the Employee Detail View. In this view, XAF creates a lookup editor for the Position reference property.

    ASP.NET Core Blazor
    ASP.NET Core Blazor detail view lookup editor
    Windows Forms
    Windows Forms detail view lookup editor

Exercise: Add an “Address” Reference Property

  1. Use the same steps to add an Address reference property to the Employee 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 the Employee 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.

  2. Run the application and open the Employee Detail View. It should now contain the Address field:

    ASP.NET Core Blazor
    ASP.NET Core Blazor Address reference property
    Windows Forms
    Windows Forms Address reference property

Next Lesson

Initialize Entity Properties

See Also