see on GitHub

How to Setup a Default Database

Database property allows you to setup a default database for a DocumentStore. Implication of setting up a default database is that each time you access Operations or create a Session without explicitly passing database on which they should operate on then default database is assumed.

Example I

// without specifying `Database`
// we will need to specify the database in each action
// if no database is passed explicitly we will get an exception
using (IDocumentStore store = new DocumentStore
{
    Urls = new[] { "http://localhost:8080/" }
}.Initialize())
{
    using (IDocumentSession session = store.OpenSession(database: "NorthWind"))
    {
        // ...
    }
    store.Maintenance.Server.Send(new CompactDatabaseOperation(new CompactSettings { DatabaseName = "NorthWind" }));
}

Example II

// when `Database` is set to `Northwind`
// created `Operations` or opened `Sessions`
// will work on `Northwind` database by default
// if no database is passed explicitly
using (IDocumentStore store = new DocumentStore
{
    Urls = new[] { "http://localhost:8080/" },
    Database = "Northwind"
}.Initialize())
{
    using (IDocumentSession northwindSession = store.OpenSession())
    {
        // ...
    }
    store.Maintenance.Send(new DeleteIndexOperation("NorthWindIndex"));


    using (IDocumentSession adventureWorksSession = store.OpenSession("AdventureWorks"))
    {
        // ...
    }
    store.Maintenance.ForDatabase("AdventureWorks").Send(new DeleteIndexOperation("AdventureWorksIndex"));
}

Remarks

Note

By default value of Database property in DocumentStore is null which means that in any actions that need a databae name we will have to specify the database.