Skip to main content
All docs
V24.2
.NET 8.0+

Initialize Entity Properties

  • 2 minutes to read

This lesson explains how to initialize properties in the newly created entity instances.

The steps below show how to add a Priority property to the DemoTask class and how to initialize the property value. For DemoTask class implementation code, see the following lesson: Set a Many-to-Many Relationship.

Note

Before you proceed, take a moment to review the previous lessons:

Step-by-Step Instructions

  1. Add the Priority property to the DemoTask class and declare the Priority enumeration:

    namespace MySolution.Module.BusinessObjects {
        //...
        public class DemoTask : BaseObject {
            //...
            public virtual Priority Priority { get; set; }
        }
    
        public enum Priority {
            Low = 0,
            Normal = 1,
            High = 2
        }
    }
    
  2. Use the code below to initialize the newly added Priority property when you create a DemoTask object:

    public class DemoTask : BaseObject {
        //..
        public override void OnCreated() {
            Priority = Priority.Normal;
        }
        //...
    }
    
  3. Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.

  4. Run the application.

    Create a new Task object. In the Task detail view, the Priority field value is Normal, as you declared in the code above.

    ASP.NET Core Blazor
    ASP.NET Core Blazor initialize property value
    Windows Forms
    Windows Forms initialize property value

    Note that XAF generates a combo box for the Priority property. The combo box items are the enumeration values you declared in step 1.

    ASP.NET Core Blazor
    ASP.NET Core Blazor enumeration combo box
    Windows Forms
    Windows Forms enumeration combo box

Next Lesson

Implement Dependent Reference Properties

See Also