Skip to main content
A newer version of this page is available. .
.NET Framework 4.5.2+

Set a One-to-Many Relationship (XPO)

  • 3 minutes to read

Tip

For .NET 5 applications, see: Set a One-to-Many Relationship (XPO, .NET 5).

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.

    [DefaultClassOptions]
    public class Contact : Person {
        //...
        private Department department;
        [Association("Department-Contacts")]
        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 {
        //...
        [Association("Department-Contacts")]
        public XPCollection<Contact> Contacts {
            get {
                return GetCollection<Contact>(nameof(Contacts));
            }
        }
    }
    

    Note

    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 Web Forms 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.

    Tutorial_BMD_Lesson6_1

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

Tip

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 21.2\Components\eXpressApp Framework\MainDemo by default. The ASP.NET Web Forms version is available online at https://demos.devexpress.com/XAF/MainDemo.

 

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

See Also