Operations: Server: How to Create a Database

Create a new database on a server.

Syntax

public CreateDatabaseOperation(
    DatabaseRecord databaseRecord,
    int replicationFactor = 1)
{
}
Parameters Type Description
databaseRecord DatabaseRecord instance of DatabaseRecord containing database configuration
replicationFactor int indicates how many nodes should contain the database

DatabaseRecord

DatabaseRecord is a collection of database configurations.

Constructor

Name Description
DatabaseRecord(string databaseName) Initialize a new database record.

Properties

Name Type Description
Disabled bool (default: false) Disable the database.
Encrypted bool (default: false) Enables database encryption.
DeletionInProgress Dictionary<string, DeletionInProgress> Mark the deletion of the database from specific nodes.
Topology DatabaseTopology By default it is null and the server will decided on which nodes to place the database according to the Replication Factor.
ConflictSolverConfig ConflictSolver Specify the strategy to resolve Conflicts.
Indexes Dictionary<string, IndexDefinition> Define Indexes
AutoIndexes Dictionary<string, AutoIndexDefinition> Define Auto Indexes
Settings Dictionary<string, string> Provide Configuration settings.
Revisions RevisionsConfiguration Set Revisions configuration.
Expiration ExpirationConfiguration Set Expiration configuration.
RavenConnectionStrings Dictionary<string, RavenConnectionString> Add Raven Connection String
SqlConnectionStrings Dictionary<string, SqlConnectionString> Add SQL Connection String
PeriodicBackups List<PeriodicBackupConfiguration> Add Backup tasks.
ExternalReplications List<ExternalReplications> Add External Replication tasks.
RavenEtls List<RavenEtlConfiguration> Add Raven ETL tasks.
SqlEtls List<SqlEtlConfiguration> Add SQL ETL tasks.
Client ClientConfiguration Set Client Configuration

Remarks

If Topology is specified, the replicationFactor will be ignored.

Example

store.Maintenance.Server.Send(new CreateDatabaseOperation(new DatabaseRecord("MyNewDatabase")));
await store.Maintenance.Server.SendAsync(new CreateDatabaseOperation(new DatabaseRecord("MyNewDatabase")));

Information

To ensure database exists before creating it we can use the following example

Example - EnsureDatabaseExists

public void EnsureDatabaseExists(IDocumentStore store, string database = null, bool createDatabaseIfNotExists = true)
{
    database = database ?? store.Database;

    if (string.IsNullOrWhiteSpace(database))
        throw new ArgumentException("Value cannot be null or whitespace.", nameof(database));

    try
    {
        store.Maintenance.ForDatabase(database).Send(new GetStatisticsOperation());
    }
    catch (DatabaseDoesNotExistException)
    {
        if (createDatabaseIfNotExists == false)
            throw;

        try
        {
            store.Maintenance.Server.Send(new CreateDatabaseOperation(new DatabaseRecord(database)));
        }
        catch (ConcurrencyException)
        {
            // The database was already created before calling CreateDatabaseOperation
        }

    }
}
public async Task EnsureDatabaseExistsAsync(IDocumentStore store, string database = null, bool createDatabaseIfNotExists = true)
{
    database = database ?? store.Database;

    if (string.IsNullOrWhiteSpace(database))
        throw new ArgumentException("Value cannot be null or whitespace.", nameof(database));

    try
    {
        await store.Maintenance.ForDatabase(database).SendAsync(new GetStatisticsOperation());
    }
    catch (DatabaseDoesNotExistException)
    {
        if (createDatabaseIfNotExists == false)
            throw;

        try
        {
            await store.Maintenance.Server.SendAsync(new CreateDatabaseOperation(new DatabaseRecord(database)));
        }
        catch (ConcurrencyException)
        {
        }
    }
}

Note

Creation of a database requires admin certificate