Mongodb FluentMongo突然出现了投掷错误
我正在使用FluentMongo和MongoDB驱动程序。我的代码在一段时间内运行良好,但在更新我的MongoCSharpDriver后,我现在在尝试查询数据库时不断遇到以下错误: 鉴别器只能为类注册,不能为接口MyLib.Services.IRepoData注册 接口Mongodb FluentMongo突然出现了投掷错误,mongodb,mongodb-.net-driver,fluent-mongo,Mongodb,Mongodb .net Driver,Fluent Mongo,我正在使用FluentMongo和MongoDB驱动程序。我的代码在一段时间内运行良好,但在更新我的MongoCSharpDriver后,我现在在尝试查询数据库时不断遇到以下错误: 鉴别器只能为类注册,不能为接口MyLib.Services.IRepoData注册 接口IRepoData只是我保存到MongoDB的所有对象使用的接口。它只是为所有东西定义了_id。这是一条正在断裂的线: var item = Collection.AsQueryable().SingleOrDefault(a =
IRepoData
只是我保存到MongoDB的所有对象使用的接口。它只是为所有东西定义了_id。这是一条正在断裂的线:
var item = Collection.AsQueryable().SingleOrDefault(a => a.Id == itemID);
有人能解释一下这个吗?如果我只是在没有lambda的情况下使用.SingleOrDefault()
,那么它工作得很好,它传递的lambda会破坏它
编辑
如果这有帮助的话
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;
}
var Collection=GetCollection();
私有MongoCollection GetCollection(),其中T:class,new()
{
字符串typeName=typeof(T).Name;
var collection=db.GetCollection(typeName,safeMode);
回收;
}
找到了!我从另一个泛型方法中调用GetCollection(),如下所示:
public T Save<T>(T item) where T : class, IRepoData, new()
{
GetCollection<T>().Save(item);
}
public T保存(T项),其中T:class,IRepoData,new()
{
GetCollection()。保存(项);
}
这导致GetCollection将T视为接口,而不是实际的实例类。GetCollection在其他任何地方都可以正常工作
对于其他有这个问题的人,我只是使用了一个像这样的低级查询<代码>Collection.FindOneAs(Query.EQ(“Id”,itemID.ToString())代码>找到了!我从另一个泛型方法中调用GetCollection(),如下所示:
public T Save<T>(T item) where T : class, IRepoData, new()
{
GetCollection<T>().Save(item);
}
public T保存(T项),其中T:class,IRepoData,new()
{
GetCollection()。保存(项);
}
这导致GetCollection将T视为接口,而不是实际的实例类。GetCollection在其他任何地方都可以正常工作
对于其他有这个问题的人,我只是使用了一个像这样的低级查询<代码>Collection.FindOneAs(Query.EQ(“Id”,itemID.ToString())代码>