RavenDB transformers will lower-case your IDs

Here’s a nice little gotcha. When RavenDB (v3.0.30155) returns data from the database, it will generally preserve the casing of the IDs (e.g. an ID of MyDocuments/1 will be returned as such), even though RavenDB itself doesn’t care about casing (e.g. if you run Session.Load<MyDocument>("mydocuments/1"), you’ll still get back the right document). However, if you write a transformer like this:

public class MyDocumentTransformer : AbstractTransformerCreationTask<MyDocument>
{
    public MyDocumentTransformer()
    {
        TransformResults = docs => docs.Select(d => new TransformedDocument
        {
            Id = d.Id
        });
    }
}

You’ll notice that when you use this transformer, any returned TransformedDocuments will have lowercase IDs like mydocuments/1. Although RavenDB doesn’t care about this, we had some code further down the line that (unfortunately) did rely on casing, which then started to fail. Fortunately, since transformers are just running C# code on the server, it’s easy to fix with something like this:

public class MyDocumentTransformer : AbstractTransformerCreationTask<MyDocument>
{
    public MyDocumentTransformer()
    {
        TransformResults = docs => docs.Select(d => new TransformedDocument
        {
            Id = d.Id.replace("mydocuments", "MyDocuments")
        });
    }
}

Which will restore your IDs to their proper-cased glory.

Something to add?

This site uses Akismet to reduce spam. Learn how your comment data is processed.