FluentMongo突然引发错误 [英] FluentMongo throwing error all of a sudden
问题描述
我正在使用FluentMongo和MongoDBCSharpDriver.我的代码可以正常工作一段时间,但是在更新MongoCSharpDriver之后,当我尝试查询数据库时,我现在不断出现此错误:
I am using FluentMongo and the MongoDBCSharpDriver. My code was working fine for a while, but after updating my MongoCSharpDriver, I now I keep getting this error when I try to query the database:
鉴别器只能为类注册,不能为接口MyLib.Services.IRepoData注册."
"Discriminators can only be registered for classes, not for interface MyLib.Services.IRepoData."
接口IRepoData
只是我用于保存到MongoDB的所有对象的接口.它只是为所有内容定义_id.这是断行:
The interface IRepoData
is just one that I use for all my objects saved to MongoDB. It just defines _id for everything. Here is the line that is breaking:
var item = Collection.AsQueryable().SingleOrDefault(a => a.Id == itemID);
有人可以阐明这一点吗?如果我只使用没有lambda的.SingleOrDefault()
,那么它可以正常工作,它会传递一个破坏它的lambda.
Can anyone shed some light on this one? If I just use .SingleOrDefault()
with no lambda then it works fine, its passing a lambda that breaks it.
编辑
以防万一...
var Collection = GetCollection<MyClass>();
private MongoCollection<T> GetCollection<T>() where T : class, new()
{
string typeName = typeof(T).Name;
var collection = db.GetCollection<T>(typeName, safeMode);
return collection;
}
推荐答案
找到了!我从另一个通用方法中调用GetCollection(),如下所示:
Found it! I was calling GetCollection() from within another generic method, like this:
public T Save<T>(T item) where T : class, IRepoData, new()
{
GetCollection<T>().Save(item);
}
这导致GetCollection将T视为接口,而不是实际的实例类. GetCollection在其他任何地方都可以正常工作.
This caused GetCollection to see T as the interface instead of the actual instance class. GetCollection works fine anywhere else.
对于其他有此问题的人,我只使用了这样的低级查询... Collection.FindOneAs<T>(Query.EQ("Id", itemID.ToString()));
For anyone else with this problem, I just used a low level query like this instead... Collection.FindOneAs<T>(Query.EQ("Id", itemID.ToString()));
这篇关于FluentMongo突然引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!