Created
December 27, 2013 10:56
-
-
Save benfoster/8145486 to your computer and use it in GitHub Desktop.
Generating an entity id manually in RavenDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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