Repository navigation
Entity Triggers
Declare a trigger in C# when a stored value must stay in sync with other rows or columns. SQL Server applies the rule in the same transaction as the write, including writes from another SQL client. A rollback also rolls back the change made by the trigger.
- BlueprintEntityTrigger
-
Prepared Triggers
- PreparedTrigger_MaintainCount counts related rows, such as products per brand.
- PreparedTrigger_MaintainHierarchyLevel maintains the depth of each row in a tree.
- PreparedTrigger_MaintainValue stores a calculated decimal value, such as a price with VAT.
- PreparedTrigger_MaintainValueHistory records daily changes to a stored value.
- PreparedTrigger_MaintainParentMinimum stores the minimum of dated child values on the parent.
- PreparedTrigger_MaintainFirstValue copies a value from the first child row in sort order.
BlueprintEntityTrigger is the abstract base class for entity triggers. Each trigger exposes these members.
| Member | Purpose |
|---|---|
Name |
Identifies the trigger or provides the prefix for a group of SQL triggers. |
PropertyName |
Identifies the mapped entity property used by the rule. |
Conditions |
Lists the data events the rule handles using EBlueprintEntityTriggerConditions. The available events are Insert, Update, and Delete. |
SqlStatement |
Exposes the primary SQL statement once generated. Prepared triggers generate it during installation. |
Install(BlueprintDatabase database) |
Installs the trigger in the specified database. |
Override getDatabaseTriggers() on the source entity and return a List<BlueprintEntityTrigger>. SQL Server registration discovers the lists on registered entities and their mapped relationships, then installs each list in order after creating the schema. Use a trigger name that is unique within its SQL schema. For EF writes to a table with triggers, set UseSqlOutputClause = false on its [Entity] attribute.
The prepared-trigger pages use independent examples and share these imports.
using Blueprint.Entities;
using Blueprint.Entities.Annotations;
using Blueprint.Entities.Triggers;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;The Blueprint.Entities.Triggers.PreparedTriggers namespace contains the built-in SQL Server rules. Each rule's page explains which properties it needs and when it runs.
Registration creates missing triggers. The count, first-value, hierarchy-level, calculated-value and parent minimum rules also populate existing rows when they install. The value-history rule begins recording with the first qualifying change after installation. Prepared triggers also support explicit Install(database) calls for registered entities. Manage any trigger already present in the database with an explicit SQL migration when its definition needs to change. Schema Synchronization