6

I have the following mapping:

    public class TimeLineEntityMap : BaseEntityMap<TimeLineEntity>
    {
        public TimeLineEntityMap()
        {
            Table("time_line_entity");
            Map(x => x.Message);
            Map(x => x.ResearchId, "research_id");//.Cascade.All().Not.LazyLoad();
            ReferencesAny(x => x.EntityRef)
                .AddMetaValue<EmailEntity>(typeof(EmailEntity).Name)
                .AddMetaValue<UrlEntity>(typeof(UrlEntity).Name)
                .AddMetaValue<PhoneEntity>(typeof(PhoneEntity).Name)
                .EntityTypeColumn("entity_type")
                .IdentityType<long>()
                .EntityIdentifierColumn("entity_ref_id")
                .Not.LazyLoad();
        }
    }

when fetching from the database EntityRef comes as a proxy.

TimeLineEntity res = timeLineRepository.Find(x => x.Id == id);
JsonConvert.SerializeObject(res);

the JsonConvert is throwing:

Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'ManifestModule' with type 'System.Reflection.RuntimeModule'. Path 'Data[0].EntityRef._proxyFactoryInfo._getIdentifierMethod.Module.Assembly'.

this is my json settings:

     x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
     x.SerializerSettings.ContractResolver = new NHibernateContractResolver();
    public class NHibernateContractResolver : CamelCasePropertyNamesContractResolver
    {
        protected override JsonContract CreateContract(Type objectType)
        {
            if (typeof(NHibernate.Proxy.INHibernateProxy).IsAssignableFrom(objectType))
                return base.CreateContract(objectType.BaseType);
            else
                return base.CreateContract(objectType);
        }
    }
SexyMF
  • 10,657
  • 33
  • 102
  • 206

1 Answers1

1

Try to add [JsonIgnore] on your model class like this:

[JsonIgnore]
public class TimeLineEntity {

}

UPDATE To get serialization of it You should resolve circular reference of map.

Below links will help you.(May be your answer already existed here.)

JSON.NET Error Self referencing loop detected for type

Resolve circular references from JSON object

Stringify (convert to JSON) a JavaScript object with circular reference

http://blogs.microsoft.co.il/gilf/2011/10/17/avoiding-circular-reference-for-entity-in-json-serialization/

Good luck.

Amir Christian
  • 597
  • 6
  • 20