Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RavenDB throws casting exception after models namespace change

Tags:

.net

ravendb

In the database, I have already stored hundreds of documents. Now the system architecture has changed and (among others) models was migrated into different namespace (in different assembly).

Below, metadata of sample document is shown:

enter image description here

and code I'm using to fetch such document:

var configuration = documentSession.Load<One.Social.Core.Entities.Setting>("Setting");

which throws casting exception:

[InvalidCastException: Unable to cast object of type 'One.QA.Core.Entities.Setting' to type 'One.Social.Core.Entities.Setting'.]

UPDATE:

Similiar error but from NewtonsoftJson rises, while I have collection of specified type inside the dosument, which now changed.

In database I have Question document, which contains a list of Answers:

enter image description here

In the code, the type looks like that:

namespace One.Social.Ask.Web.Models
{
    public class Question
    {        
        public string Content { get; set; }
        public IList<One.Social.Ask.Web.Models.Answer> Answers { get; set; }        
    }
}

Answers namespace changed. In addition, now it's derived from IList<>, no ICollection<>. I don't need the $type meta now, it should be:

enter image description here.

While it is a list now, an error rises because of the old $type information:

Newtonsoft.Json.JsonSerializationException: Error resolving type specified in JSON 'System.Collections.ObjectModel.Collection`1[[One.QA.Core.Entities.Answer, One.QA.Core]], mscorlib'. ---> Newtonsoft.Json.JsonSerializationException: Could not find type 'System.Collections.ObjectModel.Collection`1[[One.QA.Core.Entities.Answer, One.QA.Core]]' in assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

What is the best way to migrate all documents to reflect current types names ? Is there any built in mechanism ?

Btw: I'm using RavenDB - Build #960

like image 315
jwaliszko Avatar asked Jun 27 '12 10:06

jwaliszko


2 Answers

I had the same problem and ended up doing this:

Advanced.DatabaseCommands.UpdateByIndex(
    "Raven/DocumentsByEntityName",
        new IndexQuery {Query = "Tag:Album"},
        new []{ new PatchRequest() { 
            Type = PatchCommandType.Modify, 
            Name = "@metadata", 
            Nested= new []{ 
                new PatchRequest{
                    Name= "Raven-Clr-Type",
                    Type = PatchCommandType.Set,
                    Value = "Core.Model.Album, Core" }}}},
        false);
like image 149
Rasmus Avatar answered Nov 14 '22 05:11

Rasmus


Jarek, The reason for the issue is that you HAVE both types. If you remove the QA type, it will just work. Or, you can do as Wyatt suggested and force this.

like image 3
Ayende Rahien Avatar answered Nov 14 '22 04:11

Ayende Rahien