【问题标题】:Polymorphism problem in MongoDb with NoRM driver带有 NoRM 驱动程序的 MongoDb 中的多态性问题
【发布时间】:2011-06-14 11:58:40
【问题描述】:

我有课

[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 应该不是问题。我真的必须制作 Content 来实现 IConvertible 吗?

【问题讨论】:

    标签: c# mongodb polymorphism norm


    【解决方案1】:

    你可以在查询之后进行转换:

    mongo.GetCollection<Area>().AsQueryable().Cast<Content>().FirstOrDefault()
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-05
        • 2017-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多