C# driver: "Member 'StudentID' of class 'BCL.BusinessObjects.EventWrapper' cannot use element name 'StudentID' because it is already being used by member 'StudentID'."

734 views
Skip to first unread message

Dan Terrill

unread,
Jul 30, 2011, 3:05:45 PM7/30/11
to mongod...@googlegroups.com

Hey Folks,

 

I have an interesting problem here. I get the error message "Member 'StudentID' of class 'BCL.BusinessObjects.EventWrapper' cannot use element name 'StudentID' because it is already being used by member 'StudentID'." when I try to serialize an EventWrapper object. I found this issue: https://jira.mongodb.org/browse/CSHARP-180 (“Misleading exception message”) that suggests it’s due to a duplicate BsonElement name anywhere in the class.

 

The thing is, the class doesn’t have any [BsonElement] attributes anywhere. None in the classes it contains, either. And the class is in a Silverlight assembly, so I can’t reference the Mongo assemblies to put in any BsonElement attributes.

 

Help me Obi Wan Kenobi, you’re my only hope!

 

 

Dan Terrill

Chief Development Engineer

C8 Sciences

5 Science Park

New Haven, CT 06511

dan.t...@c8sciences.com

www.c8sciences.com

941-626-2831

 

Dan Terrill

unread,
Jul 30, 2011, 3:17:33 PM7/30/11
to mongod...@googlegroups.com

Ok, I’ve figured out the problem. It seems the EventWrapper class inherited from a base class  and hid some of its properties. The serializer must have gotten confused.

 

You can reproduce it like this:

 

 public class BaseClass

    {

        public Guid ID { getset; }

    }

    public class DerivedClass : BaseClass

    {

        public Guid ID { getset; }

    }

 

 

A temporary fix was to remove the  inheritance. That’s not good long-term, though. Any idea how I can work around it?

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Robert Stam

unread,
Jul 30, 2011, 8:36:51 PM7/30/11
to mongodb-user
Your best bet is to change the name of one of the Id properties to
avoid the name collision (the compiler probably gave you a warning
about the Id in the derived class hiding the Id in the base class).

If you must keep the duplicated names, you can always initialize the
class map in code rather than using attributes. For example, you could
do this to change the element name of the Id property in the derived
class:

BsonClassMap.RegisterClassMap<DerivedClass>(cm => {
cm.AutoMap();
cm.GetMemberMap(c => c.Id).SetElementName("DerivedId");
});

As a general rule, anything you can do with attributes there is always
a way of doing in code. While attributes are often the easiest way of
configuring serialization, it isn't always possible to use attributes,
but it is always possible to use code based class map initialization.

On Jul 30, 3:17 pm, Dan Terrill <dan.terr...@c8sciences.com> wrote:
> Ok, I've figured out the problem. It seems the EventWrapper class inherited from a base class  and hid some of its properties. The serializer must have gotten confused.
>
> You can reproduce it like this:
>
>  public class BaseClass
>     {
>         public Guid ID { get; set; }
>     }
>     public class DerivedClass : BaseClass
>     {
>         public Guid ID { get; set; }
>     }
>
> A temporary fix was to remove the  inheritance. That's not good long-term, though. Any idea how I can work around it?
>
> From: mongod...@googlegroups.com [mailto:mongod...@googlegroups.com] On Behalf Of Dan Terrill
> Sent: Saturday, July 30, 2011 3:06 PM
> To: mongod...@googlegroups.com
> Subject: [mongodb-user] C# driver: "Member 'StudentID' of class 'BCL.BusinessObjects.EventWrapper' cannot use element name 'StudentID' because it is already being used by member 'StudentID'."
>
> Hey Folks,
>
> I have an interesting problem here. I get the error message "Member 'StudentID' of class 'BCL.BusinessObjects.EventWrapper' cannot use element name 'StudentID' because it is already being used by member 'StudentID'." when I try to serialize an EventWrapper object. I found this issue:https://jira.mongodb.org/browse/CSHARP-180("Misleading exception message") that suggests it's due to a duplicate BsonElement name anywhere in the class.
>
> The thing is, the class doesn't have any [BsonElement] attributes anywhere. None in the classes it contains, either. And the class is in a Silverlight assembly, so I can't reference the Mongo assemblies to put in any BsonElement attributes.
>
> Help me Obi Wan Kenobi, you're my only hope!
>
> Dan Terrill
> Chief Development Engineer
> C8 Sciences
> 5 Science Park
> New Haven, CT 06511
> dan.terr...@c8sciences.com<mailto:dan.terr...@c8sciences.com>www.c8sciences.com<http://www.c8sciences.com>
> 941-626-2831
>
> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com<mailto:mongod...@googlegroups.com>.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com<mailto:mongodb-user+unsubscribe@g ooglegroups.com>.
Reply all
Reply to author
Forward
0 new messages