Initialize a Property After Creating an Object (EF 6)
- 3 minutes to read
In this lesson, you will learn how to set the default value for a particular property of a business class. For this purpose, the Priority property will be added to the DemoTask class created in the Set a Many-to-Many Relationship (EF 6) lesson. To initialize it, assign a value to this property in the constructor.
Note
Before proceeding, take a moment to review the following lessons:
Add the Priority property to the DemoTask class and declare the Priority enumeration, as shown below:
In the Module.cs (Module.vb) file, register the Priority enumeration type in the constructor of your ModuleBase descendant using the EnumProcessingHelper.RegisterEnum method as follows.
Use the code below to initialize the newly added Priority property when a DemoTask object is created.
public class DemoTask : Task { public DemoTask() : base() { //... Priority = Priority.Normal; } //... }
The constructor will be executed when the new DemoTask object is created. As a result, the Priority property will be initialized with the specified value.
Run the WinForms or ASP.NET Web Forms application. Create a new DemoTask object by selecting DemoTask in the drop-down list of the New () button. (In the Detail View that represents the newly created DemoTask object, note that the Priority property is set to Normal, as declared in the code above.) Notice that the enumeration property is automatically displayed by the combo box editor.
You can see the code demonstrated in this lesson in the MySolution.Module | Business Objects | DemoTask.cs (DemoTask.vb) file of the EF Demo (Code First) installed with XAF. By default, the EF Demo (Code First) application is installed in %PUBLIC%\Documents\DevExpress Demos 21.2\Components\eXpressApp Framework\EFDemoCodeFirst.
Next Lesson: Implement Dependent Reference Properties (EF 6)