see on GitHub

Session : Opening a Session

To open a synchronous session, use the OpenSession method from DocumentStore. If you prefer working in an asynchronous manner, use OpenAsyncSession.

Syntax

There are three overloads of OpenSession / OpenAsyncSession methods

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

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

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

// Open session for a specified database
IAsyncDocumentSession OpenAsyncSession(string database);

IAsyncDocumentSession OpenAsyncSession(SessionOptions options);

The first method is an equivalent of doing

store.OpenSession(new SessionOptions());
store.OpenAsyncSession(new SessionOptions());

The second method is an equivalent of doing

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

Parameters
options OpenSessionOptions Options containing information such as name of database and RequestExecutor.
Return Value
IDocumentSession / IAsyncDocumentSession Instance of a session object.

Options

string Database { get; set; }

bool NoTracking { get; set; }

bool NoCaching { get; set; }

RequestExecutor RequestExecutor { get; set; }

TransactionMode TransactionMode { get; set; }

Options
Database string Name of database that session should operate on. If null then default database set in DocumentStore is used.
NoTracking bool Indicates if session should not keep track of the changes. Default: false. More here.
NoCaching bool Indicates if session should not cache responses. Default: false. More here.
RequestExecutor RequestExecutor (Advanced) Request executor to use. If null default one will be used.
TransactionMode TransactionMode Sets the mode for the session. By default it is set to SingleNode, but session can also operate 'ClusterWide'. You can read more about Cluster-Wide Transactions here.

Example I

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

Example II - Disabling Entities Tracking

using (IDocumentSession session = store.OpenSession(new SessionOptions
{
    NoTracking = true
}))
{
    Employee employee1 = session.Load<Employee>("employees/1-A");
    Employee employee2 = session.Load<Employee>("employees/1-A");

    // because NoTracking is set to 'true'
    // each load will create a new Employee instance
    Assert.NotEqual(employee1, employee2);
}
using (IAsyncDocumentSession session = store.OpenAsyncSession(new SessionOptions
{
    NoTracking = true
}))
{
    Employee employee1 = await session.LoadAsync<Employee>("employees/1-A");
    Employee employee2 = await session.LoadAsync<Employee>("employees/1-A");

    // because NoTracking is set to 'true'
    // each load will create a new Employee instance
    Assert.NotEqual(employee1, employee2);
}

Remarks

Important

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