Details
Description
Consider the following implementation:
public class Foo
|
{
|
public ObjectId Id { get; set; }
|
public string Name { get; set; }
|
}
|
|
public class Bar
|
{
|
public ObjectId Id { get; set; }
|
public ObjectId FooId { get; set; }
|
|
[BsonIgnore]
|
public Foo Foo { get; set; }
|
}
|
When saving a Bar instance into the Collection the Foo property is ignored as expected. But the property is also being ignored when deserializing Bar.
return await db.GetCollection<Bar>("Bar")
|
.Aggregate()
|
.Lookup("Foo", "FooId", "_id", "Foo")
|
.Unwind("Foo")
|
.As<Bar>()
|
.ToListAsync(cancellationToken);
|
The code above does not work since the driver is ignoring the property Foo of Bar's class.
'Element 'Foo' does not match any field or property of class Bar.'
|
Why [*BsonIgnored*] is being used in deserialization?
Interestingly, if a change the Bar class as following (using ShouldSerialize method) the code works as expected:
public class Bar
|
{
|
public ObjectId Id { get; set; }
|
public ObjectId FooId { get; set; }
|
|
public Foo Foo { get; set; }
|
public bool ShouldSerializeFoo() { return false; }
|
}
|
Any thoughts on that?