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
using (IDocumentStore store = new DocumentStore
{
Urls = new[] { "http://your_RavenDB_server_URL" }
// Default database is not set
}.Initialize())
{
// Specify the 'Northwind' database when opening a Session
using (IDocumentSession session = store.OpenSession(database: "NorthWind"))
{
// Session will operate on the 'Northwind' database
}
// Specify the 'Northwind' database when sending an Operation
store.Maintenance.ForDatabase("Northwind").Send(new DeleteIndexOperation("NorthWindIndex"));
}
Example II
using (IDocumentStore store = new DocumentStore
{
Urls = new[] { "http://your_RavenDB_server_URL" },
// Default database is set to 'Northwind'
Database = "Northwind"
}.Initialize())
{
// Using the default database
using (IDocumentSession northwindSession = store.OpenSession())
{
// Session will operate on the default 'Northwind' database
}
// Operation for default database
store.Maintenance.Send(new DeleteIndexOperation("NorthWindIndex"));
// Specify the 'AdventureWorks' database when opening a Session
using (IDocumentSession adventureWorksSession = store.OpenSession(database: "AdventureWorks"))
{
// Session will operate on the specifed 'AdventureWorks' database
}
// Specify the 'AdventureWorks' database when sending an Operation
store.Maintenance.ForDatabase("AdventureWorks").Send(new DeleteIndexOperation("AdventureWorksIndex"));
}
Remarks
By default value of DefaultDatabase
property in DocumentStore
is null
which means that actions will be executed on <system>
database.