Is this bug ?…got reason behind circular reference … how to solve but?

谁说我不能喝 提交于 2019-12-04 21:09:58

This is a feature that you are using incorrectly.

You should never serialize the LINQ to SQL (or Entity Framework) classes. Even though Microsoft has placed [DataContract] and other attributes on these classes, they should not be serialized.

Instead, design a set of classes that correctly matches what you want to have serialized. For instance:

public class Question
{
    public int ID {get;set;}
    public string Text {get;set;}
    public List<Answer> Answers {get;set;}
}

public class Answer
{
    public string Text {get;set;}
}

Populate instances of these classes from your database classes, and serialize these data transfer classes instead.

BTW, this is the Data Transfer Object pattern.

Mark your classes with [DataContract(IsReference = true)] to allow serialization of circular references

It is a feature :)

The root of the problem is that JSON does not support circular references (although Entity Framework does).

So when you transfer data to the client in JSON you need to decide on the heirarchy you want to use.

Your solution with the [ScriptIgnore] is probably the best way to solve it. Probably best to place it on "Questions" in Answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!