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
Add the
Priority
property to theDemoTask
class and declare thePriority
enumeration:namespace MySolution.Module.BusinessObjects { //... public class DemoTask : BaseObject { //... public virtual Priority Priority { get; set; } } public enum Priority { Low = 0, Normal = 1, High = 2 } }
Use the code below to initialize the newly added
Priority
property when you create aDemoTask
object:public class DemoTask : BaseObject { //.. public override void OnCreated() { Priority = Priority.Normal; } //... }
Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.
Run the application.
Create a new
Task
object. In the Task detail view, thePriority
field value isNormal
, as you declared in the code above.- ASP.NET Core Blazor
- Windows Forms
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
- Windows Forms
Next Lesson
Implement Dependent Reference Properties