I’ve got the following model:
public class Competitor
{
public virtual int CompetitorId { get; set; }
public virtual string TeamName { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual DateTime BirthDate { get; set; }
public virtual IEnumerable<CompetitorBest> CompetitorBests { get; set; }
}
public class CompetitorBest
{
public virtual int ResultId { get; set; }
public virtual string BestTypeName { get; set; }
public virtual string IndoorOutdoor { get; set; }
public virtual int Season { get; set; }
public virtual string DisciplineName { get; set; }
public virtual string ResultValue { get; set; }
}
When trying to save it to my MongoDB instance, I get the following error:
Maximum serialization depth exceeded (does the object being serialized have a circular reference?).
I’m using automapping (ie- not set any class mapping)
I can’t see what could be wrong?
There’s no circular reference?
Ok,
I’m submitting this as an answer, mainly because it works – ie, allows me to save the document as required.
As @chebum correctly pointed out, the problem is actually with the fact I’m pulling the entities using that model using nHibernate first.
This means that my IEnumerable< CompetitorBest > on my Competitor object, is actually of type NHibernate.Collection.Generic.PersistentGenericBag
As correctly pointed out by @chebum in the comments, this will have a reference to the original object, therefore creating a circular reference.
Rather than create a mongodb class map (which is what I should probably do.. feel free to answer) this is what I did to get things working for now.
Changed my Competitor class to this:
Then in my nHibernate Class Map:
Again, I’m sure there’s a better way of doing it (perhaps with a mongodb class mapping??)
But i’ve added this as it works for me… for now.