Reference Properties in Entity Framework

The example below illustrates how to implement Reference (Foreign Key, Complex Type) Properties in an Entity Framework Code First class.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Editors;
using DevExpress.Persistent.Base;
// ...
public class ReferencedPropertiesObject : IXafEntityObject, IObjectSpaceLink {
    public int Id { get; set; }
    // 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; }

    #region IXafEntityObject members
    public void OnCreated() {
        EmbeddedDetailViewObject = ObjectSpace.CreateObject<EmbeddedDetailViewObject>();
        ExpandPropertiesObject = ObjectSpace.CreateObject<ExpandPropertiesObject>();
    public void OnLoaded() { }
    public void OnSaving() { }

    #region IObjectSpaceLink members
    public IObjectSpace ObjectSpace { get; set; }

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.