The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.
.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

How to: Implement Many-to-Many Relationships

  • 3 min to read

There are three types of relationships between objects. The type of a relationship that is created depends upon how related objects are defined. To learn about a particular relationship type, click a corresponding link below.

 

This article demonstrates how to create many-to-many relationships in databases created by XPO. If you are using XPO to map to existing databases, refer to the approach described in the Generating Persistent Objects for Existing Data Tables topic.

XPO can handle Many-to-Many relationship between objects. In the following code example, the Location class might contain several Departments and every Department can in turn span several Locations.


// Represents the Location class that contains its name and information 
// about the departments at the location.
public class Location: XPObject {
    public Location(Session session) : base(session) { }
    public string Name;
    // Apply the Association attribute to mark the Departments property 
    // as the many end of the LocationsDepartments association.
    [Association("LocationsDepartments")]
    public XPCollection<Department> Departments { 
        get { return GetCollection<Department>("Departments"); }
    }
}

// Represents the Department class that contains its name 
// and references all the locations of the department's offices.
public class Department: XPObject {
    public Department(Session session) : base(session) { }
    public string Name;
    // Apply the Association attribute to mark the Locations property 
    // as the many end of the LocationsDepartments association.
    [Association("LocationsDepartments")]
    public XPCollection<Location> Locations { 
        get { return GetCollection<Location>("Locations"); }
    }
}
Important

Do not modify the XPCollection property declaration demonstrated above. Manipulating the collection or introducing any additional settings within the declaration may cause unpredictable behavior.

To create data use the following code.


using DevExpress.Xpo;

// ...
public partial class Form1 : Form {
    UnitOfWork uw;
    public Form1() {
        InitializeComponent();
        uw = new UnitOfWork();
        CreateData(uw);
    }
    private void CreateData(UnitOfWork uw) {
        Department dep = new Department(uw);
        dep.Name = "Department A";
        Location loc = new Location(uw);
        loc.Name = "USA";
        dep.Locations.Add(loc);
        loc = new Location(uw);
        loc.Name = "UK";
        dep.Locations.Add(loc);
        uw.CommitChanges();
    }
}

The code listed above is sufficient for XPO to work properly: it will generate all necessary intermediate tables and relationships.

To use the association's name as the name of a junction table, set the AssociationAttribute.UseAssociationNameAsIntermediateTableName property to true.

See Also