可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I got the result from the collection in MongoDB, the structure is the same as below
[DataContract]
public class Father
{
[BsonId]
[DataMember]
public MongoDB.Bson.ObjectId _id { get; set; }
[DataMember]
public string Id { get; set; }
[DataMember]
public List<Child> childs { get; set; }
}
[DataContract]
public class Child
{
[DataMember]
public string Id { get; set; }
[DataMember]
public int Name { get; set; }
}
When I try this:
List<Father> f = result.ToList();
It calls Element 'Id' does not match any field or property of the class Model.Child
I think it just takes 'Id' as something else.
How can I deal with it? Thank you
回答1:
I resolved similar issue by adding [BsonIgnoreExtraElements] on top of the class declaration. ObjectId is internally maintained by MongoDB which is not needed at the App level, hence in the class declaration I don't require this field. Hope this would help someone
回答2:
I faced to same problem.Same error occured at var data = query.ToList();
var collection = db.GetCollection<Product>("Products");
var query =
from e in collection.AsQueryable<Product>()
where e.name == "kalem"
select e;
var data = query.ToList();
and my insert was this:
var collection = db.GetCollection<Product>("Products");
collection.InsertBatch(products);
I solved as below.
ObjectId id = new ObjectId();
var collection = db.GetCollection<Product>("Products");
collection.InsertBatch(products);
id = pr.Id;
and I added id
to Product class as below
Product Class
class Product
{
public ObjectId Id { get; set; }
public string name { get; set; }
public string category { get; set; }
public double price { get; set; }
public DateTime enterTime { get; set; }
}
回答3:
var conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) };
ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);
This works just perfectly!
[BsonIgnoreExtraElements] also worked well, but, if you want to add any other ConventionRegistry like CamelCaseElementNameConvention, then, it overrides the Attribute one and the same exception occurs. Not sure if that could also be achieved using some other attribute.
回答4:
I was using a dynamic list (List) to build a filter and was receiving a similar error. I added these lines to my data class to fix the problem.
[BsonId]
public ObjectId Id { get; set; }
回答5:
this work for my case: add label
[DataMember]
[BsonElement("songName")]
onto the elements:
[BsonIgnoreExtraElements]
public class Music
{
[BsonId]
[DataMember]
public MongoDB.Bson.ObjectId _id { get; set; }
[DataMember]
public string Id { get; set; }
[DataMember]
[BsonElement("songName")]
public string SongName { get; set; }
[DataMember]
[BsonElement("artistName")]
public string ArtistName { get; set; }}
回答6:
Your Child class should probably specify it inherits Father
public class Child: Father { ... }
Your Father class should probably add known type attribute (for WCF ).
[DataContract]
[KnownType(typeof(Child))]
public class Father
If this is a MongoCollection("fathers") that you save / fetch through, then you may need to register a class map for each expected child type.
if (!BsonClassMap.IsClassMapRegistered(typeof(Child)))
{
BsonClassMap.RegisterClassMap<Child>(
cm => { cm.AutoMap(); });
}
As @alexjamesbrown mentioned, you are not required to name the id field on your poco object '_id'.
The idea with inheritance is to inherit. So using the Father's "id" field (whatever it is named) should suffice. It's not clear why your Father class has both Id and _id properties. One of them is probably not necessary.
回答7:
all you have to do is removing the [DataMember] on the ObjecId attribute and bind the Id to the ObjectId _id.
so your class should look like this :
[DataContract]
public class Father
{
[BsonId]
public MongoDB.Bson.ObjectId _id { get; set; }
[DataMember]
public string Id {
get { return _id.ToString(); }
set { _id = ObjectId.Parse(value); }
}
[DataMember]
public List<Child> childs { get; set; }
}
ps : in your case the child id must be generated manually, if you want it to be an objectId(mongo), you will have do the same trick
finally, to deserialized the object, you should use the newtonsoft.json reference and do like this
Father = JsonConvert.DeserializeObject<Father>(response.Content);