Initialize Business Object Properties (EF Core)
- 2 minutes to read
This lesson explains how to initialize properties in the newly created business class instances.
For this purpose, this topic describes how to add the Priority
property to the DemoTask
class created in the Set a Many-to-Many Relationship (EF Core) lesson. Then initialize the new property.
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 : Task { //... Priority priority; public Priority Priority { get => priority; set => SetPropertyValue(ref priority, value); } } public enum Priority { Low = 0, Normal = 1, High = 2 } }
In the Module.cs file, call the
EnumProcessingHelper.RegisterEnum
method to register thePriority
enumeration type in the constructor of yourModuleBase
descendant:using DevExpress.Data.Filtering; //... public sealed partial class MySolutionModule : ModuleBase { public MySolutionModule() { EnumProcessingHelper.RegisterEnum(typeof(MySolution.Module.BusinessObjects.Priority)); //... } //... }
Use the code below to initialize the newly added
Priority
property when you create aDemoTask
object:public class DemoTask : Task { public DemoTask() : base() { //... 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. In the Task detail view, the Priority field’s value is Normal, as you declared in the code above.
Note that XAF generates a combo box for the
Priority
property. The combo box items are the enumeration values you declared in step 2.
Next Lesson
Implement Dependent Reference Properties (EF Core)