How to setup a default database?
DefaultDatabase
property allows you to setup a default database for a DocumentStore
. Implication of setting up a default database is that each time you access Commands or create a Session without explicitly passing database on which they should operate on then default database is assumed.
Example I
// without specifying `DefaultDatabase`
// created `DatabaseCommands` or opened `Sessions`
// will work on `<system>` database by default
// if no database is passed explicitly
using (IDocumentStore store = new DocumentStore
{
Url = "http://localhost:8080/"
}.Initialize())
{
IDatabaseCommands commands = store.DatabaseCommands;
using (IDocumentSession session = store.OpenSession())
{
// ...
}
IDatabaseCommands northwindCommands = commands.ForDatabase("Northwind");
using (IDocumentSession northwindSession = store.OpenSession("Northwind"))
{
// ...
}
}
Example II
// when `DefaultDatabase` is set to `Northwind`
// created `DatabaseCommands` or opened `Sessions`
// will work on `Northwind` database by default
// if no database is passed explicitly
using (IDocumentStore store = new DocumentStore
{
Url = "http://localhost:8080/",
DefaultDatabase = "Northwind"
}.Initialize())
{
IDatabaseCommands northwindCommands = store.DatabaseCommands;
using (IDocumentSession northwindSession = store.OpenSession())
{
// ...
}
IDatabaseCommands adventureWorksCommands = northwindCommands.ForDatabase("AdventureWorks");
using (IDocumentSession adventureWorksSession = store.OpenSession("AdventureWorks"))
{
// ...
}
IDatabaseCommands systemCommands = northwindCommands.ForSystemDatabase();
using (IDocumentSession systemSession = store.OpenSession(Constants.SystemDatabase))
{
// ..
}
}
Remarks
Note
By default value of DefaultDatabase
property in DocumentStore
is null
which means that actions will be executed on <system>
database.