1

In the RavenDb documentation I can get the document Id if I pass in my object:

string orderId = session.Advanced.GetDocumentId(order); // "orders/1"

but what I would like is to just pass in the type and the object's id value instead, like:

string orderId = session.Advanced.GetDocumentId(typeof(Order), 1); // "orders/1"

Is this at all possible? If so, how? I'm trying to avoid having to pull the object out of the database before I delete it. I'm having RavenDb generate the collection names so I don't want to make any assumptions about the name.

Rush Frisby
  • 11,388
  • 19
  • 63
  • 83

1 Answers1

3

You can get this from the document store's conventions, using the FindFullDocumentKeyFromNonStringIdentifier method.

Here's a simple extension method that will help:

public static string GetStringIdFor<T>(this IDocumentStore documentStore, int id)
{
    return documentStore.Conventions.FindFullDocumentKeyFromNonStringIdentifier(id, typeof(T), false);
}

Now you can do this:

string orderId = documentStore.GetStringIdFor<Order>(1);

Or, if you don't happen to have access to the document store at that point in your code, you can grab it from the session:

string orderId = session.Advanced.DocumentStore.GetStringIdFor<Order>(1);
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575