将EF模型序列化为Json时的循环引用 [英] Circular reference while serializing EF model to Json

查看:343
本文介绍了将EF模型序列化为Json时的循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题在很多时候都是很多关于这个问题的问题,但是没有一个解决了我的问题。



我正在使用MVC 5与实体框架6和Newtonsoft.Json。



我有这种例外情况:

  Service =>员工=>服务

当我尝试序列化一个服务对象在我看来,像这样:

  var arr = @ Html.Raw(@ JsonConvert.SerializeObject(Model.Services)) ; 

在序列化类型的对象时,我获得了循环引用。



我在这里找到的所有答案都说这是很难解决的,我应该添加

  GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings 
.PreserveReferencesHandling = PreserveReferencesHandling.All;

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;

在我的 Global.asax 文件中。 / p>

嗯,我没有,只是不行。我在MSDN上看了一堆文章,他们都说同样的话。我不知道为什么,但它对我来说不起作用。



我可以让它工作的唯一方法是在我的...中创建整个序列化上下文控制器:

  var settings = new JsonSerializerSettings 
{
PreserveReferencesHandling = PreserveReferencesHandling.All,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
};


var serializer = JsonSerializer.Create(settings);
var msmStream = new MemoryStream();
var txtWriter = new StreamWriter(msmStream);
var writer = new JsonTextWriter(txtWriter){Formatting = Formatting.Indented};
serializer.Serialize(writer,services);

var json = Encoding.ASCII.GetString(msmStream.GetBuffer());

但是,这是一个可怕的可怕解决方案,特别是如果我从我的视图模型序列化一个属性在飞行中的视野。它也击败了全局配置的整个目的。



有没有人面临这个问题?

解决方案

您需要将DefaultSettings更改为新的。

  JsonConvert.DefaultSettings =()= >新的JsonSerializerSettings 
{
PreserveReferencesHandling = PreserveReferencesHandling.All,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
};

来源


I know that are tons of questions related to this topic all over SO but none of them solved my problem.

I'm using MVC 5 with Entity Framework 6 and Newtonsoft.Json.

I have the usual scenario for this exception:

Service => Staff => Service

When I try to serialize a service object in my view, like this:

var arr = @Html.Raw(@JsonConvert.SerializeObject(Model.Services));

I get the "circular reference was detected while serializing an object of type..." exception.

All the answers I found here say it is wasy to solve, I should just add

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
            .PreserveReferencesHandling = PreserveReferencesHandling.All;

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
        .ReferenceLoopHandling = ReferenceLoopHandling.Serialize;

in my Global.asax file.

Well, I did, and it just doesn't work. I read a bunch of articles on MSDN an they all say the same thing. I don't know why, but it just doesn't work for me.

The only way I could make it work, was to create the whole serialization context in my controller:

var settings = new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.All,
    ReferenceLoopHandling = ReferenceLoopHandling.Serialize
};


var serializer = JsonSerializer.Create(settings);
var msmStream = new MemoryStream();
var txtWriter = new StreamWriter(msmStream);
var writer = new JsonTextWriter(txtWriter) { Formatting = Formatting.Indented };
serializer.Serialize(writer, services);

var json = Encoding.ASCII.GetString(msmStream.GetBuffer());

However, this is a terrible terrible solution, specially if I'm serializing a property from my view model on the fly in the view. It also defeats the whole purpose of a "global configuration".

Has anybody faced this problem?

解决方案

You need to change the DefaultSettings to new ones.

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.All,
    ReferenceLoopHandling = ReferenceLoopHandling.Serialize
};

Source

这篇关于将EF模型序列化为Json时的循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆