Search code examples
c#ravendbprojection

How to handle projections in RavenDB


Given domain model...

public class Entity
{
    public int Id { get; set; }
    public Category Category { get; set; }
}

public class Category
{
    public string Title { get; set; }
}

... I want to project results of a select query to this view model:

public class EntityViewModel
{
    public int Id { get; set; }
    public string CategoryTitle { get; set; }
}

I have tried the following query:

var viewModel = (from entity in _documentSession.Query<Entity>()
                select new EntityViewModel
                            {
                                Id = entity.Id,
                                CategoryTitle = entity.Category.Title
                            }.ToList();

The result of this is only partially correct: the Id is set, the CategoryTitle is not. I understand this behaviour is by design, but I suspect there is an API to handle this scenario.

How should such a projection be handled in RavenDB?

Update: I am using build 1.0.573 in embedded mode.

Updated 2: I have forked RavenDB repository, added a failing test to demonstrate this behaviour and created a pull request (#444). Will post more info as I find out.


Solution

  • Looks like it is actually a bug. See pull request #444 for more information.

    I will update this answer when this is fixed in a stable release.

    Fixed in the current stable release.