Skip to main content
All docs
V22.1
.NET Framework 4.5.2+

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Set a One-to-Many Relationship (XPO)

  • 2 minutes to read

This lesson explains how to create a one-to-many relationship between business objects and how XAF generates the UI for such a relationship.

#Step-by-Step Instructions

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

    [DefaultClassOptions]
    public class Contact : Person {
        //...
        private Department department;
        [Association("Department-Contacts")]
        public Department Department {
            get {return department;}
            set {SetPropertyValue(nameof(Department), ref department, value);}
        }
        //...
    }
    

    Refer to the following help topic for information on the Association attribute: Set a Many-to-Many Relationship (XPO).

  2. 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 {
        //...
        [Association("Department-Contacts")]
        public XPCollection<Contact> Contacts {
            get {
                return GetCollection<Contact>(nameof(Contacts));
            }
        }
    }
    
  3. Run the application.

    Open the Department detail view. You can see the Contacts group. To add objects to the Contacts collection, use the New or Link button in this tab. The Link button allows users to add references to existing Contact objects.

    blazor one-to-many relation new link button

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

    blazor one-to-many relation unlink button

Tip

If you create a new Department and then create a new Contact in the Contacts collection, the 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. To change this behavior, use the XafApplication.LinkNewObjectToParentImmediately property. When the property value is true, the application creates a link and saves it immediately after you click New.

#Next Lesson

Initialize a Property After Creating an Object (XPO)

See Also