5

我有课

[MongoDiscriminated]
public abstract class Content
{
    public int? Id { get; set; }
    public int? ParentId { get; set; }
    public string Slug { get; set; }
    public string Path { get; set; }
    public string Title { get; set; }
}

public class Area : Content
{
}

像这样的查询有效

var item = mongo.GetCollection<Area>().AsQueryable().FirstOrDefault();

但是当我进行类似的查询时

var item = mongo.GetCollection<Content>().AsQueryable().FirstOrDefault();

我得到一个 InvalidCastException

Object must implement IConvertible.

怎么了?将 Area 转换为 Content 应该不是问题。我真的必须制作内容来实现 IConvertible 吗?

4

2 回答 2

0

您可以在查询后进行转换:

mongo.GetCollection<Area>().AsQueryable().Cast<Content>().FirstOrDefault()
于 2011-05-10T04:02:10.270 回答
0

刚刚在 github 上提交了一个 pull request 来修复这个异常:

https://github.com/atheken/NoRM/pulls

于 2011-08-30T18:51:08.350 回答