Getting Started

SQL Persistence

Target Version: NServiceBus 7.x

Stores NServiceBus data in various relational database engines, without the need for an intermediate ORM, using only a configured DbConnection.

Persistence at a glance

For a description of each feature, see the persistence at a glance legend.

Storage TypesSagas, Outbox, Subscriptions
TransactionsLocal database transactions or distributed transactions when available
Concurrency controlPessimistic concurrency
Scripted deploymentSQL scripts generated at compile time, can be promoted outside build directory.
InstallersInstallers execute the generated scripts, which can be enabled in development.


Supported SQL implementations

NuGet Packages

The SQL Persister requires the NServiceBus.Persistence.Sql package, which contains:

  • APIs for manipulating EndpointConfiguration.
  • Runtime implementations of saga, timeouts, subscriptions, and outbox persisters.
  • Attributes used to define compile-time configuration settings.
  • [Version 4.3+] An MSBuild target that generates the required SQL installation scripts at compile time.
  • Optionally runs SQL installation scripts at endpoint startup for development purposes. See Installer Workflow.

Other packages

  • NServiceBus.Persistence.Sql.MsBuild - In version 4.2 and below, this package must also be referenced to generate scripts. In version 4.3 and above, this package is deprecated, as the generation capability is built in to the main package.
  • NServiceBus.Persistence.Sql.ScriptBuilder - This package contains the APIs that enable the generation of SQL installation scripts using code outside of a compile context. It is not intended for general usage, and in future releases, the API may evolve in ways that do not follow semantic versioning.

Script creation

SQL installation scripts are created as a compile-time output alongside a project's binary outputs. Additionally, these scripts can be promoted to a directory under source control so that differences can be easily tracked and analyzed. To learn more, see Controlling Script Generation.


Looping & branching statements are not allowed

SqlPersistenceTask: Looping & branching statements are not allowed in a ConfigureHowToFindSaga method.

The ConfigureHowToFindSaga method can only contain statements that invoke ConfigureMapping<T>. Check if there is a null check or if the calling of ConfigureHowToFindSaga was done via the ?. Null-conditional operator and remove the ‘?.’ operator when present as the mapper will never be null.


Related Articles