Controlling script generation

Component: Sql Persistence
NuGet Package NServiceBus.Persistence.Sql (3.x)
Target NServiceBus Version: 6.x

SQL installation scripts are created at compile time by the NServiceBus.Persistence.Sql.MsBuild NuGet package.

Scripts will be created in the directory format of [CurrentProjectDebugDir]\NServiceBus.Persistence.Sql\[SqlDialect].

For example for a project named ClassLibrary built in Debug mode the following directories will be created.

  • ClassLibrary\bin\Debug\NServiceBus.Persistence.Sql\MsSqlServer
  • ClassLibrary\bin\Debug\NServiceBus.Persistence.Sql\MySql
  • ClassLibrary\bin\Debug\NServiceBus.Persistence.Sql\Oracle

Scripts will also be included in the list of project output files. This means those files produced will be copied to the output directory of any project that references it.

Scripts creation can configured via [SqlPersistenceSettings] applied to the target assembly.

SQL scripts are not copied to the publish directory with dotnet publish.
Projects using project.json are not supported. The project.json approach was an experiment by Microsoft at a new project system that was not based on MSBuild. Since project.json did not support running MSBuild files shipped inside a NuGet the SQL Persistence script creation does not work. This experiment has since been abandoned. To fix this either migrate back to the old Visual Studio 2015 project format (.csproj and packages.config) or migrate to the new Visual Studio 2017 project format. dotnet-migrate can help migrating to the new .csproj format.

To produce all scripts

[assembly: SqlPersistenceSettings(
    MsSqlServerScripts = true,
    MySqlScripts = true,
    OracleScripts = true,
    PostgreSqlScripts = true)]

To produce only MS SQL Server scripts

[assembly: SqlPersistenceSettings(MsSqlServerScripts = true)]

To produce only MySQL scripts

[assembly: SqlPersistenceSettings(MySqlScripts = true)]

To produce only Oracle scripts

[assembly: SqlPersistenceSettings(OracleScripts = true)]

To produce only PostgresSql scripts

[assembly: SqlPersistenceSettings(PostgreSqlScripts = true)]

Toggle script creation

At compile time the SQL persistence validates that sagas are of the correct base type and match the required conventions. This can be problematic when mixing persistences. For example using the SQL persistence for timeouts but another persister for saga storage. As such it is possible to selectively control what SQL scripts are generated at compile time, and as a side effect avoid the convention validation of sagas.

Disable outbox script creation

[assembly: SqlPersistenceSettings(
    ProduceOutboxScripts = false)]

Disable saga script creation

[assembly: SqlPersistenceSettings(
    ProduceSagaScripts = false)]

Disable subscriptions script creation

[assembly: SqlPersistenceSettings(
    ProduceSubscriptionScripts = false)]

Disable timeout script creation

[assembly: SqlPersistenceSettings(
    ProduceTimeoutScripts = false)]


As stated above, scripts are created in the target project output directory. Generally this directory will be excluded from source control. To add created scripts to source control they can be "promoted".

The target directory will be deleted and recreated as part of each build. Be sure to choose a path that is for script promotion only.

Some token replacement using MSBuild variables is supported.

  • $(SolutionDir): The directory of the solution.
  • $(ProjectDir): The directory of the project

All tokens are drive + path and include the trailing backslash \.

[assembly: SqlPersistenceSettings(
    ScriptPromotionPath = "$(SolutionDir)PromotedSqlScripts")]

The path calculation is performed relative to the current project directory. So, for example, a value of PromotedSqlScripts (with no tokens) would evaluate as $(ProjectDir)PromotedSqlScripts.

Last modified