Wednesday, January 2, 2013

RavenDB: Change the primary key convention

Every document in RavenDB should have a unique identifier. By default it searches for properties named Id. If it couldn’t find such a property an unique identifier is created for you behind the scenes(but is not available through a property on your object).

But what if you don’t like this convention?

For example what if you want to call your identifier properties ‘key’ instead of ‘id’. Changing this convention is easy in RavenDB:

var documentStore = new EmbeddableDocumentStore
{
DataDirectory = "App_Data",
UseEmbeddedHttpServer = true
};

var store= documentStore.Initialize();

store.Conventions.FindIdentityProperty = prop =>
{
return
prop.Name
.Equals("key",StringComparison.InvariantCultureIgnoreCase);
};

No comments: