【问题标题】:NHibernate json proxy serialization with with ReferencesAnyNHibernate json 代理序列化与 ReferencesAny
【发布时间】:2020-01-22 02:50:46
【问题描述】:

我有以下映射:

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

JsonConvert 正在投掷:

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

这是我的 json 设置:

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

【问题讨论】:

  • 那么你的问题是什么?
  • @AmirChristian 查看异常消息
  • 请分享您的模型类..
  • 你需要jsonignore

标签: json nhibernate


【解决方案1】:

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

[JsonIgnore]
public class TimeLineEntity {

}

更新 要获得它的序列化,您应该解决地图的循环引用。

以下链接将对您有所帮助。(您的答案可能已经存在于此。)

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/

祝你好运。

【讨论】:

  • 但是我需要序列化这些数据
猜你喜欢
  • 1970-01-01
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 1970-01-01
  • 2014-08-19
  • 1970-01-01
相关资源
最近更新 更多