Dapper and MongoDB persistence providers
The Dapper and MongoDB providers connect Elsa's workflow, identity, management, and runtime services to relational databases and MongoDB. They expose the same persistence contracts while applying tenant scope, storing workflow state, keeping runtime values, and maintaining the data rules needed for safe operation.
Provider roles
The providers split persistence work by data role rather than by one large store. Dapper maps records to SQL tables through database-specific query rules. MongoDB maps documents to collections through a shared document store. Both providers support the same main areas of the system.
| Area | Responsibility |
|---|---|
| Identity | Store users, applications, and roles with tenant-aware uniqueness. |
| Management | Store workflow instances and support search, paging, summaries, status changes, and deletion. |
| Runtime | Store serialized key-value data and other execution state. |
| Shared persistence | Apply tenant visibility and ownership rules to reads and writes. |
These providers are alternatives to the EF Core and version-next persistence pages: persistence, persistence-efcore, and persistence-vnext. The application selects the provider that matches its database model and operational needs.
Dapper provider
The Dapper provider is a relational persistence layer. Its shared store turns provider-neutral record operations into parameterized SQL and uses a dialect to account for differences between supported databases. This lets identity, management, and runtime stores share query behavior without making each feature own database-specific rules.
Tenant filtering is part of the normal store operation. A default tenant can be represented by either an empty tenant ID or a legacy NULL value. Tenant-agnostic operations are explicit because they can cross the normal tenant boundary.
The query layer also provides the common behavior needed by the management and runtime features: filtering, ordering, paging, counts, inserts, updates, upserts, and guarded deletes. Query values are passed as parameters so the provider can preserve the database contract while avoiding direct value insertion into SQL.
References: src/…/Store.cs• L15-20 src/…/Store.cs• L217-231 src/…/Store.cs• L350-383 src/…/Store.cs• L467-480 src/…/Store.cs• L538-560 src/…/ParameterizedQueryBuilderExtensions.cs• L122-169 src/…/ParameterizedQueryBuilderExtensions.cs• L231-252
MongoDB provider
The MongoDB provider is a document persistence layer. Its shared store wraps MongoDB collections and supplies common add, save, update, query, count, and delete behavior to the feature stores. The feature registers the MongoDB client, database, collection naming policy, class maps, options, and startup services.
MongoDB entity reads in a tenant include documents from that tenant and shared documents marked with the tenant-agnostic ID. Tenant-scoped deletes exclude shared documents. Writes keep the document's ownership rules, so a named tenant cannot silently take ownership of a document belonging to another tenant.
Direct collection access bypasses these tenant filters. Code that uses the underlying collection must therefore apply its own scope when it manages documents outside the shared store contract.
References: src/…/MongoDbFeature.cs• L19-58 src/…/MongoDbStore.cs• L12-29 src/…/MongoDbStore.cs• L43-99 src/…/MongoDbStore.cs• L462-540 src/…/Elsa.Persistence.MongoDb.csproj• L1-15
Persistence responsibilities
The provider stores are adapters for Elsa contracts. They preserve the workflow and identity model while choosing a relational row or a MongoDB document as the durable form.
Workflow instance management
Workflow instance stores provide the management view of running and completed workflows. They find instances by identity, definition, status, correlation, execution state, time, or search text. They also return full instances, summaries, IDs, counts, and pages so management clients can choose the amount of state they need.
When a workflow instance is saved, its workflow state is serialized for storage and restored when the instance is read. The stores also support status recovery: an eligible instance can be marked interrupted, while finished instances are not moved back into an active state.
The two providers express the same management behavior through different query models. Dapper uses parameterized SQL and projects records into the requested result shape. MongoDB uses collection queries and projections before it applies ordering and paging.
References: src/…/DapperWorkflowInstanceStore.cs• L18-31 src/…/DapperWorkflowInstanceStore.cs• L33-49 src/…/DapperWorkflowInstanceStore.cs• L127-160 src/…/DapperWorkflowInstanceStore.cs• L178-202 src/…/WorkflowInstanceStore.cs• L18-43 src/…/WorkflowInstanceStore.cs• L117-170
Runtime key-value storage
The runtime key-value store keeps serialized values by key. Runtime code can save a value, find one or many values, match an exact key or a key prefix, and delete a key.
The Dapper store protects ownership during concurrent saves. It updates only a row owned by the current tenant, detects conflicts with another tenant or the tenant-agnostic scope, and retries a short sequence of updates when concurrent changes occur. The MongoDB store uses the key as its save selector and relies on the shared document store for tenant-aware matching.
This ownership rule matters because a key is globally identifiable while its value is tenant-scoped. A failed save must report a scope conflict instead of changing another tenant's runtime state.
References: src/…/KeyValueStore.cs• L16-63 src/…/KeyValueStore.cs• L65-106 src/…/KeyValueStore.cs• L108-161 src/…/KeyValueStore.cs• L11-52 src/…/MongoDbStore.cs• L75-99
Identity role storage
Identity role storage keeps role names and permissions within tenant scope. A role name can be reused by different tenants, but one tenant must not have two roles with the same name under the identity service's case-insensitive rule.
The Dapper role store checks ownership before it updates a role. If the same ID belongs to another tenant or to a tenant-agnostic role, the save fails rather than moving that role into the current tenant. MongoDB moves from the older store-wide role-name rule to a compound tenant-and-name rule during startup.
References: src/…/RoleStore.cs• L13-57 src/…/V3_10.cs• L9-31 src/…/CreateIndices.cs• L12-30 src/…/CreateIndices.cs• L127-183
Schema and startup changes
The Dapper provider uses versioned migrations for durable SQL schema changes. The MongoDB provider uses startup registration and index checks because collections and indexes are managed by the database rather than by a relational migration chain.
Dapper migrations
The Dapper migration set adds the runtime key-value table and the serialized bookmark option needed by runtime processing. These changes are existence-guarded, and rollback leaves them in place so a hand-created workaround or its data is not removed.
The identity migration adds a unique (TenantId, Name) role index after checking for same-tenant duplicates. The check treats empty and NULL tenant IDs as the default tenant and compares names without case. If conflicting rows exist, the migration stops without changing rows and reports the IDs that need operator action.
Migration database aliases keep raw SQL checks aligned with the identifier rules of SQLite, SQL Server, MySQL, PostgreSQL, and Oracle. The alias list also controls the database-specific branch used for date-time columns.
References: src/…/V3_9.cs• L8-40 src/…/V3_10.cs• L34-79 src/…/V3_10.cs• L81-140 src/…/MigrationDatabases.cs• L3-57
MongoDB startup registration
MongoDB startup prepares the client and database from the configured connection string. It also installs serializers and class maps before stores handle workflow, runtime, and identity data.
Identity startup creates indexes for applications, users, and roles. Role index migration creates the per-tenant unique index before it removes the legacy store-wide name index. It keeps a weaker or unexpected index visible through a warning or startup failure rather than silently weakening role uniqueness.
References: src/…/MongoDbFeature.cs• L39-58 src/…/CreateIndices.cs• L33-47 src/…/CreateIndices.cs• L135-183
Serialization boundaries
Serialization converts in-memory workflow values into forms that the selected database can store and later restore. The MongoDB provider defines this boundary explicitly for values that do not have a safe default BSON representation.
MongoDB value serialization
MongoDB startup registers serializers for polymorphic objects, types, variables, versions, JSON values, and flow scopes. A variable is mapped through Elsa's serialization type registry so its value and type information can be restored through the normal workflow model.
JSON nodes use a tagged BSON form for objects, arrays, and primitive values. The reader also accepts older document, array, and primitive forms. This lets the provider read existing data while using one clear representation for new values.
References: src/…/ConfigureMongoDbSerializers.cs• L14-37 src/…/VariableSerializer.cs• L12-63 src/…/JsonNodeBsonSerializer.cs• L10-32 src/…/JsonNodeBsonSerializer.cs• L75-145 src/…/JsonNodeBsonSerializer.cs• L148-217
Persistence flow
The provider flow starts with an Elsa contract and ends with a tenant-scoped durable record. This diagram shows the shared path and the provider-specific storage boundary.
flowchart LR
Contract[Elsa persistence contract] --> Store[Provider store]
Store --> Scope[Tenant scope and ownership]
Scope --> Choice{Selected provider}
Choice --> SQL[Parameterized SQL rows]
Choice --> BSON[MongoDB documents]
SQL --> Result[Stored or restored workflow data]
BSON --> Result
For workflow instances, the store also crosses a serialization boundary. It serializes state before a save and restores state before returning a workflow instance. For runtime values, it preserves the serialized payload while enforcing key ownership.
References: src/…/DapperWorkflowInstanceStore.cs• L127-146 src/…/KeyValueStore.cs• L23-63 src/…/WorkflowInstanceStore.cs• L157-176 src/…/KeyValueStore.cs• L15-38 src/…/MongoDbStore.cs• L75-124
Change checklist
When these files or sections are changed, remember to consider these:
- Keep Dapper migration provider aliases aligned with the identifier quoting and date-time rules of every supported SQL database.
- Keep role uniqueness checks consistent between the Dapper unique index and MongoDB's tenant-and-name index transition.
- Preserve the distinction between tenant-visible shared records and records that a tenant may delete or update.
- Keep MongoDB serializer registration aligned with every polymorphic workflow value that can cross a persistence boundary.
- Keep runtime key ownership behavior consistent when changing save, retry, or duplicate-key handling.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Existing same-tenant duplicate role names during the Dapper migration | The unique index is not created and startup migration fails. | Resolve the reported role rows, then run the migration again. |
A weak MongoDB (TenantId, Name) role index with no store-wide unique name index | MongoDB startup refuses to proceed because role names would have no uniqueness guard. | Drop the weak index and restart so the unique index can be created. |
| A runtime key belongs to another tenant or the tenant-agnostic scope | The save fails instead of overwriting another scope's value. | Use the owning tenant or choose a different key. |
| A MongoDB operation uses the raw collection without tenant filtering | It can read, change, or delete data outside the normal tenant scope. | Use MongoDbStore or apply an explicit tenant filter. |
| A workflow instance is already finished when interruption recovery runs | The instance is not moved back to running state. | Treat finished instances as terminal. |
Key files
| File | Why to read it |
|---|---|
| src/…/Store.cs | Understand the shared SQL store, tenant filtering, and write operations. |
| src/…/DapperWorkflowInstanceStore.cs | See the relational workflow management contract. |
| src/…/KeyValueStore.cs | See runtime key ownership and concurrent save behavior. |
| src/…/V3_10.cs | Understand tenant-aware role uniqueness and migration safety. |
| src/…/MongoDbStore.cs | Understand MongoDB tenant visibility, ownership, and collection access. |
| src/…/MongoDbFeature.cs | See MongoDB client, database, options, and startup registration. |
| src/…/WorkflowInstanceStore.cs | See the document workflow management contract. |
| src/…/CreateIndices.cs | Understand identity indexes and the role-index transition. |
| src/…/ConfigureMongoDbSerializers.cs | See the startup serializer boundary. |