You are currently browsing legacy 4.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: How to Get and Modify Entity Metadata

When a document is downloaded from the server, it contains various metadata information like ID or current change-vector. This information is stored in a session and is available for each entity using the GetMetadataFor method from the Advanced session operations.

Get the Metadata

IMetadataDictionary GetMetadataFor<T>(T instance);

Parameters
instance T Instance of an entity for which metadata will be returned.
Return Value
IMetadataDictionary Returns the metadata for the specified entity. Throws an exception if the instance is not tracked by the session.

Example

var employee = session.Load<Employee>("employees/1-A");
var metadata = session.Advanced.GetMetadataFor(employee);

Modify the Metadata

After getting the metadata from session.Advanced.GetMetadataFor you can modify it just like any other dictionary.

Note

Keys in the metadata that starting with @ are reserved for RavenDB use

Example I

var user = new User
{
    Name = "Idan"
};

session.Store(user);
var metadata = session.Advanced.GetMetadataFor(user);
metadata["Permissions"] = "ReadOnly";
session.SaveChanges();

Example II

var user = session.Load<User>("users/1-A");
var metadata = session.Advanced.GetMetadataFor(user);

metadata["Permissions"] = "ReadAndWrite";

session.SaveChanges();