Bootstrap

学习008-02-01-10 Create a Calculated Property(创建计算属性)

Create a Calculated Property(创建计算属性)

This lesson explains how to create calculated properties.
本课介绍如何创建计算属性。

For this purpose, add a Payment entity class with the following properties:
为此,添加具有以下属性的Payment实体类:

  • Rate (a persistent property)(速率(持久属性))
  • Hours (a persistent property)(小时(持久属性))
  • Amount (a non-persistent, calculated property: Amount = Rate * Hours)(Amount(非持久性计算属性:Amount=速率*小时))

Note
Before you proceed, take a moment to review the previous lesson:
在继续之前,请花点时间回顾上一课:

Step-by-Step Instructions(分步说明)

1.In the MySolution.Module\Business Objects folder, create a Payment class. Replace the generated class declaration with the following code:
在MySolutions. Module\Business Objects文件夹中,创建一个Payment类。将生成的类声明替换为以下代码:

C#

using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using System.ComponentModel.DataAnnotations.Schema;

[DefaultClassOptions]
public class Payment : BaseObject
{
    //Use this attribute to specify the display format pattern for the property value.
    [ModelDefault("DisplayFormat", "{0:c}")]
    public virtual double Rate { get; set; }
    public virtual double Hours { get; set; }

    //Use this attribute to exclude the property from database mapping.
    [NotMapped]
    [ModelDefault("DisplayFormat", "{0:c}")]
    public double Amount
    {
        get { return Rate * Hours; }
    }
}

The Amount property does not require a set accessor. Value calculation takes place in the get accessor.
Amount属性不需要设置访问器。值计算在get访问器中进行。

2.Register the Payment type in DbContext:
在DbContext中注册付款类型:

C#

public class MySolutionEFCoreDbContext : DbContext {
    //...
    public DbSet<Payment> Payments { get; set; }
}

3.Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.
添加迁移并更新数据库。有关详细信息,请参阅以下部分:使用DBMS:设置迁移。

4.Run the application. Select the Payment item in the navigation control, and click New. In the Payment Detail View, change the Rate and Hours properties to see how this affects the Amount property.
运行应用程序。在导航控件中选择付款项,然后单击新建。在付款详细信息视图中,更改费率和小时属性以查看这如何影响金额属性。

ASP.NET Core Blazor
在这里插入图片描述

Windows Forms
在这里插入图片描述

Next Lesson(下一课)

Add a Simple Action
添加简单操作

;