Skip to main content
.NET 8.0+

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