Skip to main content
A newer version of this page is available. .

Set a One-to-Many Relationship (XPO)

  • 3 minutes to read

In this lesson, you will learn how to set a one-to-many relationship between business objects. The Contact and Department business objects will be related by a one-to-many relationship. For this purpose, the Contacts property will be added to the Department class representing the “Many” part of the relationship. You will then learn the basics of automatic user interface construction for referenced objects.

  • To implement the “One” part of the Department-Contacts relationship, decorate the Contact class’ Department property with the AssociationAttribute.

    public class Contact : Person {
        private Department department;
        public Department Department {
            get {return department;}
            set {SetPropertyValue(nameof(Department), ref department, value);}

    For information on the Association attribute, refer to the Set a Many-to-Many Relationship (XPO) lesson.

  • To implement the “Many” part of the Department-Contacts relationship, add the Contacts property to the Department class and decorate this property with the Association attribute.

    public class Department : BaseObject {
        public XPCollection<Contact> Contacts {
            get {
                return GetCollection<Contact>(nameof(Contacts));


    CodeRush includes a number of Code Templates that help generate business classes or their parts with a few keystrokes. To learn about the Code Templates for eXpress Persistent Objects, refer to the following help topic: XPO and XAF Templates.

  • Run the WinForms or ASP.NET application. Invoke a Detail View for a Department object (see the previous lesson “Set a Many-to-Many Relationship (XPO)”). You can see the Contacts group. To add objects to the Contacts collection, use the New (button_new) or Link (link_btn) buttons in this tab. The Link button allows for the adding of references to existing Contact objects.


    To remove a reference to an object from this collection, use the Unlink (unlink_img) button.


If you create a new Department and then create a new Contact in the Contacts collection, an associated Department is not immediately visible in the Detail View of the newly created Contact. The link between these objects is added later, when you save the Contact. You can change this behavior using the XafApplication.LinkNewObjectToParentImmediately property. When it is set to true, the link will be created and saved immediately after you click New.

You can see the code demonstrated in this lesson in the MySolution.Module | Business Objects | Contact.cs (Contact.vb) file of the Main Demo installed with XAF. The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 20.2\Components.NET Core Desktop Libraries\eXpressApp Framework\MainDemo by default. The ASP.NET Web Forms version is available online at


Next Lesson: Initialize a Property After Creating an Object (XPO)

See Also