Skip to content

Database SchemaSynchronization

github-actions[bot] edited this page Dec 4, 2026 · 1 revision

Schema Synchronization

RegisterEntities compares the registered model with SQL Server and applies supported schema changes immediately. Blueprint does not require a separate EF migration command or migration-history table for this synchronization.

Register entities

database.RegisterEntities(typeof(Product));

The database constructor creates a missing database. Registration then prepares the entity tables, related contracts, indexes, ID sequences, and any configured RAM caches. A repeated registration checks the model again.

Perform registration during application startup so that schema failures are handled before serving requests. It can also run after database initialization, but not inside an application transaction.

Supported changes

Change Behavior
Missing schema, table, column, or declared index Created when the model is valid and SQL permissions allow it.
New nullable column Added without inventing values for existing rows.
New required column on a populated table Requires an explicit usable default; otherwise registration fails.
Wider string or numeric type Applied when existing values can be preserved, such as a larger maximum length or int to bigint.
Decimal precision or scale Applied only when the new definition preserves the original integral and fractional capacity.
Making a column nullable Supported.
Making a column required Requires that existing rows contain no NULL values.
Default constraint changes Synchronized for future inserts; they are not a general data backfill.
Missing or outdated ID sequence Repaired so that generated IDs can continue beyond stored IDs.

For example, changing [MaxLength(100)] to [MaxLength(200)] is a widening change. Reducing it to 50 requires an explicit migration strategy, even when today's rows happen to fit.

Changes that need a migration

Blueprint does not automatically drop removed tables or columns. It does not infer renames, discard data, shrink incompatible types, or rewrite computed columns to match arbitrary changes.

For a C# rename that should preserve the existing SQL column, use [Column("ExistingColumnName")]. For an actual database rename or data transformation, prepare an explicit migration before registering the new model.

A removed column can still affect writes: an old required column without a usable default may reject inserts even though it is no longer in the class. Include retained columns and constraints in your migration review.

Atomicity and failures

Schema changes are protected by a SQL transaction and an application lock, with table locking where needed. If synchronization fails, schema changes in that transaction are rolled back. Blueprint retains the previous active model and removes all new registrations from the failed batch.

RAM-cache prerequisites include database-level Change Tracking and snapshot-isolation settings. These can be enabled before the schema transaction and remain enabled if a later step fails. See RAM Caching.

Registration requires appropriate SQL permissions, including creation or alteration of objects when needed. It can fail for incompatible existing data, duplicate values in a proposed unique index, invalid relationships, or unsupported type changes. Resolve the reported model or data problem before retrying.

Prepared triggers

Return prepared triggers from each entity's getDatabaseTriggers() method. Registration synchronizes tables, columns and relationships, then installs the declared triggers before loading the RAM cache. Each prepared rule installs in its own transaction. If a later rule fails, the previous active model stays available and any already committed schema and triggers can be reused on retry. See Entity Triggers.

A prepared value-history trigger starts recording on the first change after installation and preserves existing history.

Maintained decimal values

Declare a stored price with [Precision(18, 2), DatabaseGenerated(DatabaseGeneratedOption.Computed)] and a public abstract getter. Give a required generated column a database default for the initial insert. Return PreparedTrigger_MaintainValue<TEntity> from getDatabaseTriggers() and register the entities.

The formula supports decimal literals, mapped decimal or nullable decimal properties on the entity or a direct reference, and addition, subtraction, multiplication and division. SQL Server propagates nulls and rounds the result to the scale of the destination column. Installation creates any missing row and reference triggers and fills existing values in the same transaction. See Prepared value trigger for a chained calculation.

Generated IDs

Every entity has one long ID regardless of its language. IDs use per-table SQL sequences. Do not assign IDs to new entities, depend on contiguous values, or treat ID order as transaction commit order. See EShop - Checkout Tutorial.

Related topics

SQL Server · Data Annotations · RAM Caching · Troubleshooting

Clone this wiki locally