Initialize a Property After Creating an Object创建对象后初始化属性 即如何设置对象的默认值(EF)

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) lesson. To initialize it, assign a value to this property in the constructor.

在本课中,您将学习如何为 Business 类的特定属性设置默认值。为此,优先级属性将添加到在"设置多对多关系 (EF)"一课中创建的 DemoTask 类中。要初始化它,请为构造函数中的此属性赋值。

Note

Before proceeding, take a moment to review the following lessons:

  • Inherit from the Business Class Library Class (EF)
  • Set a Many-to-Many Relationship (EF)
  • Add the Priority property to the DemoTask class and declare the Priority enumeration, as shown below:

  • 注意

    在继续之前,请花点时间复习以下课程:

  • 从业务类库类 (EF) 继承

  • 设置多对多关系 (EF)

  • 将"优先级"属性添加到 DemoTask 类并声明优先级枚举,如下所示:

    复制代码
    namespace MySolution.Module.BusinessObjects {
        //...
        public class DemoTask : Task {
            //...
            public Priority Priority { get; set; }
        }
    
        public enum Priority {
            Low = 0,
            Normal = 1,
            High = 2
        }
    }
    复制代码

     

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

  • 在Module.cs(Module.vb)文件中,使用枚举处理帮助器.寄存器Enum方法,在ModuleBase后代的构造函数中注册优先级枚举类型,如下所示。

    复制代码
    using DevExpress.Data.Filtering;
    //...
    public sealed partial class MySolutionModule : ModuleBase {
        public MySolutionModule() {
            InitializeComponent();
            EnumProcessingHelper.RegisterEnum(typeof(MySolution.Module.BusinessObjects.DemoTask.Priority));
            //...
        }
        //...
    }
    复制代码

     

  • Use the code below to initialize the newly added Priority property when a DemoTask object is created.

  • 在创建 DemoTask 对象时,使用以下代码初始化新添加的"优先"属性。

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.

  • 创建新的 DemoTask 对象时,将执行构造函数。因此,将用指定的值初始化"Priority"属性。
  • Run the WinForms or ASP.NET application. Create a new DemoTask object by selecting DemoTask in the drop-down list of the New (new_dropdown_btn) 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.

  • 运行 WinForms 或ASP.NET应用程序。通过在"新建(new_dropdown_btn)"按钮的下拉列表中选择"演示任务",创建新的"演示任务"对象。(在表示新创建的 DemoTask 对象的详细信息视图中,请注意,优先级属性设置为"正常",如上述代码中声明的那样。请注意,枚举属性由组合框编辑器自动显示。

    Tutorial_BMD_Lesson12_1

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 19.2\Components\eXpressApp Framework\EFDemoCodeFirst.

您可以在 MySolution.模块中看到本课中演示的代码。业务对象 |DemoTask.cs (DemoTask.vb) 文件与 XAF 一起安装的 EF 演示(代码优先)文件。默认情况下,EF 演示(代码优先)应用程序安装在 %PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\EFDemoCodeFirst 中。

posted @   code first life  阅读(415)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示