2

Here is the information about my development environment:

MongoDB 3.0.0

MongoDB C# Driver Version 1.7.0.4714

Microsoft Visual Studio Professional 2013

.NET Framework 4.0

Here is a C# Class whose objects will be used as embedded documents:

   public class Location
   {
       public double lng { get; set; }
       public double lat { get; set; }
   }

Here is a C# Class used to represent a person's house:

   public class House
   {
       public String houseDescription { get; set; }
       public Location locOfHouse  { get; set; }
   }

We have a C# API module used by mobile application component that will set the locOfHouse = []

which means that in the MongoDB Collection called House, we could have the following document:

{
  "_id" : ObjectId("56c455ee26b49c090019b439"),
  "houseDescription" : "Multi-floor house with elevator and staircase",   
  "locOfHouse" : []
}

In my C# application, I have the following class mappting to BSON

     if (false == BsonClassMap.IsClassMapRegistered(typeof(House)))
        {
            BsonClassMap.RegisterClassMap<House>(cm =>
            {
                cm.AutoMap();
                cm.MapProperty<Location>(c => (Location)c.locOfHouse);

            });

        } 

However, the web-based C# application throws the following error when retrieving the aforementioned data:

An error occurred while deserializing the locOfHouse property of class 
House: Expected a nested document representing the serialized form of a Location 
value, but found a value of type Array instead.

How do I change my C# application code in such a way the it will Not throw the aforementioned error?

3
  • 2
    Why is the "C# API module" setting locOfHouse to an empty array? That's the broken code - I would initially try to fix that rather than making everything else work around the broken data. Commented Feb 18, 2016 at 8:28
  • It is handled by a different developer, and he wants me to just handle the empty array in my code. Commented Feb 18, 2016 at 8:30
  • 1
    You should agree on a data model. If the model is that locOfHouse is meant to be a single value, the other developer should change their code. If the model is that locOfHouse should be a collection (which seems very odd to me), you should change your House class. Trying to maintain two incompatible data models is a recipe for pain. Commented Feb 18, 2016 at 8:34

1 Answer 1

1

Change with List

public Location locOfHouse  { get; set; }

to

public List<Location> locOfHouse  { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.