Skip to main content
.NET Framework 4.5.2+

Reference Properties in EF Core

  • 2 minutes to read

The example below illustrates how to implement Reference (Foreign Key, Complex Type) Properties in an EF Core class.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Editors;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
// ...
public class ReferencedPropertiesObject : BaseObject {
    // Displayed in a lookup control: 
    public virtual ReferencedObject LookupReferencedObject { get; set; }
    // Displayed in a set of editors. Each editor represents an individual property of the referenced object: 
    [Aggregated, ExpandObjectMembers(ExpandObjectMembers.Always)]
    public virtual ExpandPropertiesObject ExpandPropertiesObject { get; set; }
    // Displayed in a Detail Property Editor that shows a referenced object's Detail View: 
    [Aggregated, EditorAlias(EditorAliases.DetailPropertyEditor)]
    public virtual EmbeddedDetailViewObject EmbeddedDetailViewObject { get; set; }
    // Displayed in a button edit that invokes a referenced object's Detail View in a separate modal window: 
    [Aggregated, ExpandObjectMembers(ExpandObjectMembers.Never)]
    public virtual PopupDetailViewObject PopupDetailViewObject { get; set; }

    public override void OnCreated() {
        EmbeddedDetailViewObject = ObjectSpace.CreateObject<EmbeddedDetailViewObject>();
        ExpandPropertiesObject = ObjectSpace.CreateObject<ExpandPropertiesObject>();

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.

Note that the reference properties should be declared as virtual in EF.

If you use Detail Property Editor for a reference property, or apply ExpandObjectMembers attribute to a reference property, it is required to initialize such a property when a new parent object is created. Otherwise, the reference property’s fields will be read-only. The initialization should be done in the following way:

The code above demonstrates these steps.

See Also