Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping a private backing field with MongoDB C#

I'm trying to get a private backing field mapped in MongoDB.
My model looks like:

public class Competitor
{
    private IList<CompetitorBest> _competitorBests;

    public virtual int CompetitorId { get; set; }

    public virtual string Name
    {
        get
        {
            if (Type == "Team")
                return TeamName;

            return FirstName + " " + LastName;
        }
    }

    public virtual IEnumerable<CompetitorBest> CompetitorBests
    {
        get { return _competitorBests.ToArray(); }
    }
}

I'm basically trying to map _competitorBests, to be CompetitorBests (which exists in my document in mongo)

Note: This model is shared by NHibernate (hence the virtual)
I can't see anything obvious in the docs.

How do I do it?

like image 995
Alex Avatar asked Jul 04 '12 15:07

Alex


1 Answers

This did the trick:

BsonClassMap.RegisterClassMap<Competitor>(cm =>
{
    cm.AutoMap();
    cm.MapField("_competitorBests").SetElementName("CompetitorBests");
});
like image 107
Alex Avatar answered Oct 28 '22 09:10

Alex