FluentMongo throwing error all of a sudden

ぃ、小莉子 提交于 2019-12-11 12:27:28

问题


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:

"Discriminators can only be registered for classes, not for interface MyLib.Services.IRepoData."

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);

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.

EDIT

In case this helps...

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;
}

回答1:


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);
}

This caused GetCollection to see T as the interface instead of the actual instance class. GetCollection works fine anywhere else.

For anyone else with this problem, I just used a low level query like this instead... Collection.FindOneAs<T>(Query.EQ("Id", itemID.ToString()));



来源:https://stackoverflow.com/questions/8055603/fluentmongo-throwing-error-all-of-a-sudden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!