Skip to main content
.NET 8.0+

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

Collection Properties in EF Core

The example below illustrates how to implement Collection Properties in an EF Core class.

using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using System.Collections.ObjectModel;

namespace DXApplication21.Module.BusinessObjects {
    [DefaultClassOptions]
    public class Employee : BaseObject {
        // ...
        // An associated collection - Tasks can be linked to and unlinked from an Employee object.
        public virtual IList<Task> Tasks { get; set; } = new ObservableCollection<Task>();
        // An aggregated collection - Addresses are a part of an Employee object
        // and cannot exist without this object.
        [Aggregated]
        public virtual IList<Address> Addresses { get; set; } = new ObservableCollection<Address>();
    }
}

Important

While it is possible to use different generic types such as ICollection<T>, IList<T>, and so on for the public/external collection property declaration, the inner collection must implement the System.Collections.Specialized.INotifyCollectionChanged interface for change notifications through the INotifyCollectionChanged.CollectionChanged event (see above). We recommend that you use ObservableCollection<T> internally as the default option.

Note that collection properties should be declared as virtual in EF Core.

See Also