C# driver RegisterClassMap throws exception in derived class after upgrade form 1.4 to 1.9.1

262 views
Skip to first unread message

Eugeniy Timofeev

unread,
May 22, 2014, 1:23:14 AM5/22/14
to mongod...@googlegroups.com

Hi, everyone!

I've just updated to mongodb c# driver 1.9.1 from 1.4. Now I've got an error when I'm trying to get document from collection. Collection has property "SomeClass", which has property of type "DerivedClass". So, DerivedClass is a 3-rd level document.

class CollectionClass
{
    int ColId;
SomeClass SomeClassProperty; } class SomeClass { DerivedClass DerivedClassProperty; } class BaseClass { string id; float floatValue; } class DerivedClass { int intValue; }

I'm using mapping like this:

BsonClassMap.RegisterClassMap<CollectionClass>(cm =>
            {
                cm.AutoMap();
                cm.SetIgnoreExtraElements(true);
                cm.SetIdMember(cm.GetMemberMap(c => c.ColId).SetIdGenerator(StringObjectIdGenerator.Instance));
           });

BsonClassMap.RegisterClassMap<SomeClass>(cm =>
            {
                cm.AutoMap();
                cm.SetIgnoreExtraElements(true);
            });     

BsonClassMap.RegisterClassMap<BaseClass>(cm =>
{
    cm.AutoMap();
    cm.MapIdMember(c => c.id);  
    cm.GetMemberMap(c => c.id).SetElementName("id"); 
    cm.GetMemberMap(c => c.floatValue).SetElementName("fv");  
}
BsonClassMap.RegisterClassMap<DerivedClass>(cm =>
{
    cm.AutoMap();
    cm.GetMemberMap(c => c.intValue).SetElementName("iv");
}

When I request document, using findOne(), I've got an error: "An error occurred while deserializing the SomeClass property of class CollectionClass: An error occurred while deserializing the DerivedClass property of class SomeClass: Element 'id' does not match any field or property of class DerivedClass.".

In C# driver version 1.4 everything works perfectlynyone has ideas?
Thanks in advance.

Eugeniy Timofeev

unread,
May 22, 2014, 2:05:50 AM5/22/14
to mongod...@googlegroups.com
As I understand problem is with mapping id field in BaseClass, cause if I ignore extra fields in derived class, I've got the same error in other derived classes.

Eugeniy Timofeev

unread,
May 22, 2014, 5:42:11 AM5/22/14
to mongod...@googlegroups.com
I've found a solution. I can't understand why, but after under the hood (or if you call "freeze") id maps to _id authomatically. So, replacing cm.MapIdMember(c => c.id); with cm.SetIdMember(null);
Reply all
Reply to author
Forward
0 new messages