Repository navigation
PreparedTrigger_MaintainValue
Use PreparedTrigger_MaintainValue<TEntity> on the entity that stores the result. Pass the destination property name and the calculation as a C# expression. The two rules below form a chain. The VAT category derives its multiplier from the rate. The product multiplies its net price by the multiplier of its assigned category.
using Blueprint.Entities.Triggers.PreparedTriggers;
using System;
using System.Linq.Expressions;
[Entity("ProductVatCategories", UseSqlOutputClause = false)]
public abstract class ProductVatCategory : BlueprintEntityDB
{
/// <summary>Defines the trigger that converts the VAT percentage into a price multiplier.</summary>
/// <returns>The prepared multiplier trigger.</returns>
public override List<BlueprintEntityTrigger> getDatabaseTriggers()
{
ParameterExpression category = Expression.Parameter(typeof(ProductVatCategory), "category");
BinaryExpression fraction = Expression.Divide(Expression.Property(category, nameof(Rate)), Expression.Constant(100m));
UnaryExpression formula = Expression.Convert(Expression.Add(Expression.Constant(1m), fraction), typeof(decimal?));
return [new PreparedTrigger_MaintainValue<ProductVatCategory>("TR_ProductVatCategories_PriceMultiplier", nameof(PriceMultiplier),
Expression.Lambda<Func<ProductVatCategory, decimal?>>(formula, category))];
}
[Precision(5, 2)]
public abstract decimal Rate { get; set; }
[Precision(5, 4), DefaultValue(typeof(decimal), "1")]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public abstract decimal PriceMultiplier { get; }
}
[Entity("Products", UseSqlOutputClause = false)]
public abstract class Product : BlueprintEntityDB
{
/// <summary>Defines the trigger that calculates the price including VAT.</summary>
/// <returns>The prepared price trigger.</returns>
public override List<BlueprintEntityTrigger> getDatabaseTriggers()
{
ParameterExpression product = Expression.Parameter(typeof(Product), "product");
MemberExpression multiplier = Expression.Property(Expression.Property(product, nameof(VatCategory)), nameof(ProductVatCategory.PriceMultiplier));
BinaryExpression formula = Expression.Multiply(Expression.Property(product, nameof(NetPrice)), Expression.Convert(multiplier, typeof(decimal?)));
return [new PreparedTrigger_MaintainValue<Product>("TR_Products_PriceWithVAT", nameof(PriceWithVAT),
Expression.Lambda<Func<Product, decimal?>>(formula, product))];
}
[Precision(18, 2)]
public abstract decimal? NetPrice { get; set; }
public abstract long VatCategoryID { get; set; }
[ForeignKey(nameof(VatCategoryID))]
public abstract ProductVatCategory VatCategory { get; set; }
[Precision(18, 2), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public abstract decimal? PriceWithVAT { get; }
}Register the entities with the multiplier source first.
database.RegisterEntities(typeof(ProductVatCategory), typeof(Product));Each call installs the missing primary and dependency triggers for its formula and backfills existing rows in one transaction. The product rule installs its primary trigger on Products and a dependency trigger on ProductVatCategories. SqlStatement exposes the primary definition. Entity API writes to these tables require mappings that disable SQL Server's direct OUTPUT clause.
With a net price of 100.00 and a rate of 24.00, the stored price is 124.00. Updating just the category’s Rate to 13.00 changes its PriceMultiplier to 1.1300 and the product’s PriceWithVAT to 113.00. The same update reaches every product in that category. Changing a single product’s net price or assigning it a different VAT category recalculates that product’s price. SQL Server’s nested triggers option must be enabled for chained rules.
The formula supports decimal literals, stored decimal or decimal? properties on the entity or a direct reference, and the operators +, -, * and /. SQL Server rounds the result to the destination column’s scale. A null net price produces a null result and a zero net price produces 0.00. Give a required generated column a database default for the initial insert, as shown on PriceMultiplier. Keep dependencies between separately installed rules acyclic.