I have a collection called Products which I am trying to enumerate using the official mongo-csharp driver. However as soon as I try to enumerate the collection (e.g. with a foreach loop) I get the following error.
"Default constructor not found for type MongoDB.Driver.MongoDBRef"
The entity class looks like this
public partial class Product
{
public BsonObjectId _id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Url { get; set; }
public int Price { get; set; }
public string Country { get; set; }
public MongoDBRef Merchant { get; set; }
}
The entry in the collection looks like the following
{
"_id" : ObjectId("4cff739fba63c20301ee5bc5"),
"Name" : "Product Name",
"Description" : "Product Description",
"Url" : "http://mysite/products/product-name",
"Price" : 1200,
"Country" : "au",
"Merchant" : {
"$ref" : "Merchant",
"$id" : ObjectId("533981033d565e640d000000")
}
}
And i'm reading it in like this.
var db = Db.Instance.GetDatabase();
var matches = db.GetCollection<Product>("Product").FindAll();
I don't get the error until I do either of the following.
var l = matches.ToList();
OR
foreach (var p in matches) {
// Do something
}