Skip to main content

Operations: How to Switch Operations to a Different Database

By default, the operations available directly in store are working on a default database that was setup for that store. To switch operations to a different database that is available on that server use the ForDatabase method.

Operations.ForDatabase

// Define default database on the store
var documentStore = new DocumentStore
{
Urls = new[] { "yourServerURL" },
Database = "DefaultDB"
}.Initialize();

using (documentStore)
{
// Use 'ForDatabase', get operation executor for another database
OperationExecutor opExecutor = documentStore.Operations.ForDatabase("AnotherDB");

// Send the operation, e.g. 'GetRevisionsOperation' will be executed on "AnotherDB"
var revisionsInAnotherDB =
opExecutor.Send(new GetRevisionsOperation<Order>("Orders/1-A"));

// Without 'ForDatabase', the operation is executed "DefaultDB"
var revisionsInDefaultDB =
documentStore.Operations.Send(new GetRevisionsOperation<Company>("Company/1-A"));
}
Parameters
databaseNamestringName of a database for which you want to get new Operations
Return Value
OperationExecutorNew instance of Operations that is scoped to the requested database

Example

OperationExecutor operations = documentStore.Operations.ForDatabase("otherDatabase");

How to Switch Maintenance Operations to a Different Database

As with Operations, by default the Maintenance operations available directly in store are working on a default database that was setup for that store. To switch maintenance operations to a different database use the ForDatabase method.

Maintenance.ForDatabase

// Define default database on the store
var documentStore = new DocumentStore
{
Urls = new[] { "yourServerURL" },
Database = "DefaultDB"
}.Initialize();

using (documentStore = new DocumentStore())
{
// Use 'ForDatabase', get maintenance operation executor for another database
MaintenanceOperationExecutor opExecutor = documentStore.Maintenance.ForDatabase("AnotherDB");

// Send the maintenance operation, e.g. get database stats for "AnotherDB"
var statsForAnotherDB =
opExecutor.Send(new GetStatisticsOperation());

// Without 'ForDatabase', the stats are retrieved for "DefaultDB"
var statsForDefaultDB =
documentStore.Maintenance.Send(new GetStatisticsOperation());
}
Parameters
databaseNamestringName of a database for which you want to get new Maintenance operations
Return Value
MaintenanceOperationExecutorNew instance of Maintenance that is scoped to the requested database

Example

MaintenanceOperationExecutor maintenanceOperations = documentStore.Maintenance.ForDatabase("otherDatabase");