将EF模型序列化为Json时的循环引用 [英] Circular reference while serializing EF model to 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屋!