[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
 [Expand]What's Installed
 [Expand].NET Core 3 Support
 [Expand]Common Concepts
 [Expand]MVVM Framework
 [Expand]Controls and Libraries
 [Collapse]Scaffolding Wizard
   Getting Started
  [Expand]Data Access Layer
   UI Generation
    How to: Affect Auto-Generated Columns and Edit Fields at the Data Model Level
    How to: Create a Custom Filter
    How to: Add a New Detail Collection
    How to: Show Validation Errors when IDataErrorInfo is Used
    How to: Show Detail Views in Separate Windows
    How to: Manage Document Properties and Open Them Manually
    How to: Implement Many-to-many Relationships Editing
    Entity Names Autogeneration
    How to: Use Projections to Create Custom Queries and Optimize Performance
    How To: Improve Performance by Using the Instant Feedback Mode
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How to: Implement Many-to-many Relationships Editing

This document describes how to implement many-to-many relations editing in an application generated with the DevExpress Scaffolding Wizard.

The resulting application supports the addition of multiple Course objects to the Student.CoursesAttending detail collection and vice versa.


A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T141250.

To learn how to automatically generate an application based on the Entity Framework model using the DevExpress Scaffolding Wizard, refer to the first lesson of the Building Office-Inspired Applications tutorial.

Entity Framework Code First allows you to create a many-to-many relations by creating the relevant entities, which hold an ICollection interface on each side of the relation.

Starting with v15.2, the scaffolding wizard automatically generates the code and UI for editing such relations.

Within the Common/ViewModel folder of the project you will find the AddRemoveDetailEntitiesViewModel (AddRemoveDetailEntitiesViewModel.cs file) class that contains all necessary logic to add/remove entities to/from detail collection.

  • The DetailEntities collection property exposes all entities in the object detail collection.
  • The SelectedEntities collection property is used for data binding and contains all items that will be removed from the detail collection when the RemoveDetailEntitiesCommand is executed.
  • The AddDetailEntities method uses IDialogService to show a dialog in which a customer can select entities to add to the object detail collection.
  • The RemoveDetailEntities method removes all entities that are in the SelectedEntities collection from the object detail collection.

Since AddRemoveDetailEntitiesViewModel is a POCO view model, the AddDetailEntitiesCommand and RemoveDetailEntitiesCommand commands are automatically generated at runtime and available for data binding.

In addition, there is a helper method in the SingleObjectViewModel class (SingleObjectViewModel.cs file) that allow you to create AddRemoveDetailEntitiesViewModel.


If the third ('junction') entity is explicitly defined and consists of exactly two properties that form the primary key, the AddRemoveJunctionDetailEntitiesViewMode is generated instead of the AddRemoveDetailEntitiesViewModel. It provides the same functional.

It is now easy to create this view model in all relevant detail view models.

The Views folder contains a user control (DetailEntitiesView.xaml, DetailEntitiesView.xaml.cs files) with a GridControl displaying a list of detail entities, and a Tool Bar with command buttons bound to commands available in the AddRemoveDetailEntitiesViewModel.

The resulting application allows you to add and remove entities to/from the Student.CoursesAttending and Course.Student collections. It also supports synchronization: for example, when a specific Course is removed from the Student.CoursesAttending collection and the detail view for that Course is open, the Students list in that detail view will be refreshed.

Is this topic helpful?​​​​​​​