Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The Mongo Id is not a "string" and the Mongo.Bson library will not serialize it to a string automatically. If you are using the Mongo.Bson library, you need to set the id property on your class to be a ObjectId type which is available in that library.</p> <p>The problem here is that the .net serializer doesn't know how to serialize the custom type ObjectId that's the mongo built in Id (not sure why). It is not marked as [Serializable] so you have to get ASP.NET to bypass it or create another class that doesn't have one and convert it to a string.</p> <p>If you need a string to use in your app, you should disable the serialization of the MongoId to xml (if that's what you're trying to do), then you can add a property like this to your class:</p> <pre><code>[XmlIgnore] public ObjectId _id { get; set; } public string MongoId { get { return _id.ToString(); } set { _id = ObjectId.Parse(value); } } </code></pre> <p>The alternative it to create a seperate mapped class to manage the return data.</p> <p>EDIT In that case you should use the "opt-in" approach. This involves decorating your class to look like the following:</p> <pre><code>[DataContract] public class Document { public ObjectId _id { get; set; } [DataMember] public string MongoId { get { return _id.ToString(); } set { _id = ObjectId.Parse(value); } } </code></pre> <p>...</p> <p>DataMember will flag only those properties for serialization. Are you using POCO classes for your "Document" object? If so the above should work fine.</p> <p>I would however recommend creating a mapped view of the "Document" object for passing out publically. In these situations youalmost always end up wanting a slightly different "contract" to your actual database entity</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload