NHibernate json代理序列化与ReferencesAny

时间:2019-09-21 08:40:17

标签: json nhibernate

我有以下映射:

thesisDegreeCB.setValue(theDegreeFromTableViewItem);

从数据库thesisDegreeCB.setValue(thesisDegreeCB.getItems().stream() .filter(degree -> theDegreeFromTableViewItem.getDegree().equals(degree.getDegree())) .findFirst().orElse(null)); 获取时作为代理。

    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();
        }
    }

EntityRef抛出:

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

这是我的json设置:

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

1 个答案:

答案 0 :(得分:1)

尝试像这样在模型类上添加[JsonIgnore]:

[JsonIgnore]
public class TimeLineEntity {

}

更新 要对其进行序列化,您应该解析map的循环引用。

下面的链接将为您提供帮助。(也许您的答案已经存在。)

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/

祝你好运。