Back to Devexpress

Initialize a Property After Creating an Object (EF 6)

expressappframework-113636-getting-started-in-depth-tutorial-winforms-webforms-business-model-design-business-model-design-with-entity-framework-6-initialize-a-property-after-creating-an-object-ef.md

latest2.8 KB
Original Source

Initialize a Property After Creating an Object (EF 6)

  • Jun 25, 2021
  • 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:

  • Inherit from the Business Class Library Class (EF 6)

  • Set a Many-to-Many Relationship (EF 6)

  • 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.

  • 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)