Create a Calculated Property
- 2 minutes to read
This topic explains how to create calculated properties.
The example code below declares a Payment entity class with the following properties:
Rate(a persistent property)Hours(a persistent property)Amount(a non-persistent, calculated property:Amount = Rate * Hours)
Step-by-Step Instructions
Navigate to the MySolution.Module\Business Objects folder. Create a
Paymentclass. Replace the auto-generated declaration with the following code:using DevExpress.ExpressApp.DC; using DevExpress.Persistent.Base; using DevExpress.Persistent.BaseImpl.EF; [DefaultClassOptions] public class Payment : BaseObject { public virtual decimal Rate { get; set; } public virtual float Hours { get; set; } // Use the `PersistentAlias` attribute to specify the property value calculation expression. // Call the `EvaluateAlias` method to access the calculated value. [PersistentAlias("Rate * Hours")] public decimal Amount { get { return EvaluateAlias<decimal>(); } } }Tip
You can apply an ImmediatePostDataAttribute to the
RateandHoursproperties. If you do so, any change to these properties immediately forces theAmountvalue to be recalculated.Register the
Paymenttype inDbContext:public class MySolutionEFCoreDbContext : DbContext { //... public DbSet<Payment> Payments { get; set; } }Run the application in debug mode with attached debugger. Select the Payment item in the navigation control and click New. In the Detail View, change the
RateandHoursproperties and note the updatedAmountvalue.