5

I have classes

[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
{
}

Query like this works

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

But when I make a query like

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

I get an InvalidCastException

Object must implement IConvertible.

What's wrong? It shouldn't be a problem to convert Area to Content. Do I really have to make Content to implement IConvertible?

Mike Koder
  • 1,898
  • 1
  • 17
  • 27

2 Answers2

0

You could just cast it after the query:

mongo.GetCollection<Area>().AsQueryable().Cast<Content>().FirstOrDefault()
Chris Shain
  • 50,833
  • 6
  • 93
  • 125
0

Just submitted a pull request on github to fix this exception:

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

Zaid Masud
  • 13,225
  • 9
  • 67
  • 88