Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created December 27, 2013 10:56
Show Gist options
  • Save benfoster/8145486 to your computer and use it in GitHub Desktop.
Save benfoster/8145486 to your computer and use it in GitHub Desktop.
Generating an entity id manually in RavenDB
public async void Run()
{
var orderId = await GenerateId<Order>();
var order = new Order(orderId, 10.99);
session.Store(order);
session.SaveChanges();
}
protected async Task<string> GenerateId<TEntity>()
{
var asyncSession = session as AsyncDocumentSession;
var entity = Activator.CreateInstance(typeof(TEntity), true);
// Generate an ID using the commands and conventions from the current session
var databaseName = asyncSession.DatabaseName;
var databaseCommands = asyncSession.AsyncDatabaseCommands;
return await asyncSession.DocumentStore.Conventions.GenerateDocumentKeyAsync(databaseName, databaseCommands, entity);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment