无法使用 MongoDBRef 枚举对象
我有一个名为 Products 的集合,我试图使用官方 mongo-csharp 驱动程序来枚举它。然而,一旦我尝试枚举集合(例如使用 foreach 循环),我就会收到以下错误。
“未找到 MongoDB.Driver.MongoDBRef 类型的默认构造函数”
实体类如下所示
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; }
}
集合中的条目如下所示
{
"_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")
}
}
我正在像这样读取它。
var db = Db.Instance.GetDatabase();
var matches = db.GetCollection<Product>("Product").FindAll();
在执行以下任一操作之前,我不会收到错误消息。
var l = matches.ToList();
或者
foreach (var p in matches) {
// Do something
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显示代码
var db = Db.Instance.GetDatabase();
应该是这样的:
比你的代码:
3.我检查了c#的mongo驱动程序的来源,我在MongoDBRef中发现了以下内容
所以我建议在你版本的mongo驱动程序中,来自mongo c#驱动程序团队的人忘记了默认构造函数。请以任何方式使用 reflector 检查构造函数是否存在。
4. 我 99% 确信您的 mongo 驱动程序版本中不存在该构造函数。因为当您开始枚举一些 mongo 集合时,mongo 驱动程序将需要数据,并且如果找不到默认构造函数,则会抛出错误。
Show code of
var db = Db.Instance.GetDatabase();
Should be something like this:
and than yous code:
3.I've checked source of mongo driver for c# and i found following in MongoDBRef
So i suggest that in you version of mongo driver guys from mongo c# driver team forgot about default constructor. In any way check please that constructor exist/not exist using reflector.
4. And i am 99% sure that constructor not present at yous version of mongo driver. Because when you start to enumerate some mongo collection mongo driver going to desirealize data and in case if default constructor not found throws error.