Environment Checks

Marten has a couple options for adding environment checks to your application that can assert on whether the Marten database(s) are in the correct state. The first way is to use Oakton as your command line parser for your application (which you are if you're using Marten's command line tooling) and take advantage of its built in environment check functionality.

To add an environment check to assert that the actual Marten database matches the configured state, just use the AddMarten().AddEnvironmentChecks() extension method that is contained in the Marten.CommandLine library.

Another option is this usage:

public static async Task use_environment_check()
{
    using var host = await Host.CreateDefaultBuilder()
        .ConfigureServices(services =>
        {
            // Do this, or your environment check assertion failures below
            // is just swallowed and logged on startup
            services.Configure<HostOptions>(options =>
            {
                options.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.StopHost;
            });

            services.AddMarten("connection string")
                .AssertDatabaseMatchesConfigurationOnStartup();
        })
        .StartAsync();
}

snippet source | anchor