You are currently browsing legacy 3.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Session: Opening a session

To open synchronous session use OpenSession method from DocumentStore or OpenAsyncSession if you prefer working in asynchronous manner.

Syntax

There are three overloads of OpenSession method

// Open session for a 'default' database configured in 'DocumentStore'
IDocumentSession OpenSession();

// Open session for a specified database
IDocumentSession OpenSession(string database);

IDocumentSession OpenSession(OpenSessionOptions options);

First method is a equivalent of doing

store.OpenSession(new OpenSessionOptions());

Second method is a equivalent of doing

store.OpenSession(new OpenSessionOptions
					  {
						  Database = databaseName
					  });

Parameters
options OpenSessionOptions Options containing information such as name of database on which session will work and credentials that will be used.
Return Value
IDocumentSession Instance of a session object that implements IDocumentSession interface.

Example I

using (IDocumentSession session = store.OpenSession())
{
	// code here
}

Example II

using (IAsyncDocumentSession session = store.OpenAsyncSession())
{
	// async code here
}

Remarks

Always remember to release session allocated resources after usage by invoking Dispose method or wrapping session object in using statement.