You can map these objects without problems, try the following:
public class ParentMap : ClassMap<Parent> { public ParentMap() { this.Table("Parents"); this.Id(x => x.Id); this.Map(x => x.Name); this.HasMany(x => x.Childs).KeyColumn("ChildId").Cascade.AllDeleteOrphan(); } } public class ChildMap : ClassMap<Child> { public ChildMap() { this.Table("Childs"); this.Id(x => x.Id); this.Map(x => x.Name); this.Map(x => x.ParentId);
When you receive objects from ISession, do not serialize it in some format, because it can be nhibernate proxy objects instead of object objects. Try to create DTO classes (Data Transfer Object) and convert these objects to a DTO object and serialize it. You will avoid circular links. For sample:
public class ParentDTO { public int Id { get; set; } public string Name { get; set; } public int ParentId { get; set; } }
And when you need to read the values to share the serialized value:
var dto = ISession.Query<Parent>() .Select(x => new ParentDTO() { Id = x.Id, Name = x.Name, ParentId = x.ParentId) .ToList();
Get this result from the data access level and try serializing the sample:
var result = Serialize(dto);
source share