Mapping a private backing field with MongoDB C#

大兔子大兔子 提交于 2019-11-30 18:11:04

问题


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?


回答1:


This did the trick:

BsonClassMap.RegisterClassMap<Competitor>(cm =>
{
    cm.AutoMap();
    cm.MapField("_competitorBests").SetElementName("CompetitorBests");
});


来源:https://stackoverflow.com/questions/11331575/mapping-a-private-backing-field-with-mongodb-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!