Question

I need some help about Raven DB and its C# API. I want to do an “order by” with a Linq query (with the Linq to Raven implementation). To do so I have to create an index to realize this action. My schema is currently normalized, but I want to denormalize it in order to add a custom property in it.

So, I have :

Entity A :

  • Some property
  • The Id of a B entity

In fact, I simply want to order my As by a B property. So I want to denormalize my A to include the B property.

So a scheme evolution is possible with RavenDB, but in order to set the B property in A I have to load the B when converting the A. And it seems impossible according to the documentation.

I don’t want to build a creepy program that will iterate on each A to load Bs but I don’t see another solution.

Any idea ?

Thank you for your time :). Julien

Was it helpful?

Solution

You should not write a creepy program, but a robust migrations project that will hold a bunch of helpful migration scripts. A simple script like the bellow will do the work for you:

public void LoadAndSaveWithDenormalizedProperty()
{
    using (var session = store.OpenSession())
    {
        int skip = 0;
        const int take = 24;
        while (true)
        {
            var items = session.Query<ItemA>()
                .Customize(customization => customization.Include(x => x.ItemBId))
                .Skip(skip)
                .Take(take)
                .ToList();
            skip += items.Count;

            foreach (item in items)
            {
                item.DenormalizedProperty = 
                       session.Load<ItemB>(item.ItemBId).PropertyToDenormalize;
            }

            session.SaveChanges();

            if (items.Count == 0)
                break;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top