Search code examples
c#.netravendb

Update RavenDB Raven-Clr-Type after moving class to new namespace


I need to update the RavenDB Raven-Clr-Type after moving multiple classes to a new namespace.

Example Old Metadata:

"@metadata": {
    "@collection": "SomeClass",
    "Raven-Clr-Type": "BO.SomeClass, BO"
}

Example New Metadata:

"@metadata": {
    "@collection": "SomeClass",
    "Raven-Clr-Type": "BO.SubNamespace.SomeClass, BO.SubNamespace"
}

One approach I've considered is loading a collection with a Query, iterate over the list and Store each object individually in the DB. This seems to work fine, but unfortunately, the storing of each object individually is for large collections very inefficient. I've tried to use RavenDB's Bulk Insert method as an alternative, but it doesn't appear to update the Raven-Clr-Type, just the "data".

The need to update the Raven-Clr-Type after a namespace change is associated with how we query by ID. We perform a strict type check when loading an object by ID, and if the namespace does not match, we return an error.

using var bulkInsertOperation = Store.BulkInsert(new BulkInsertOptions
{
    SkipOverwriteIfUnchanged = false
});
foreach (var item in list)
{
    bulkInsertOperation.Store(item);
}

My Question:

  • What would be the most effective way to accomplish my goal?

Solution

  • Send a Patch operation, this should work for you:

    var operation = store
        .Operations.Send(new PatchByQueryOperation(new IndexQuery
        {
            Query = @"from YourCollectionName update
                    {                                      
                        this['@metadata']['Raven-Clr-Type'] = 'TheNewNameSpaceYouWant';                                      
                    }"
        }));
        
    operation.WaitForCompletion();
    

    There are more examples of set-based operations here:
    https://ravendb.net/docs/article-page/6.0/csharp/client-api/operations/patching/set-based