Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That was the best solution <a href="http://code.msdn.microsoft.com/Loop-Reference-handling-in-caaffaf7" rel="noreferrer">http://code.msdn.microsoft.com/Loop-Reference-handling-in-caaffaf7</a></p> <h1>Fix 1: Ignoring circular reference globally</h1> <p><em>(I have chosen/tried this one, as have many others)</em></p> <p>The json.net serializer has an option to ignore circular references. Put the following code in <code>WebApiConfig.cs</code> file:</p> <pre><code> config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; </code></pre> <p>The simple fix will make serializer to ignore the reference which will cause a loop. However, it has limitations:</p> <p>The data loses the looping reference information The fix only applies to JSON.net The level of references can't be controlled if there is a deep reference chain</p> <p>If you want to use this fix in a non-api ASP.NET project, you can add the above line to <code>Global.asax.cs</code>, but first add:</p> <pre><code>var config = GlobalConfiguration.Configuration; </code></pre> <p>If you want to use this in <strong>.Net Core</strong> project, you can change <code>Startup.cs</code> as:</p> <pre><code> var mvc = services.AddMvc(options =&gt; { ... }) .AddJsonOptions(x =&gt; x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); </code></pre> <h1>Fix 2: Preserving circular reference globally</h1> <p>This second fix is similar to the first. Just change the code to:</p> <pre><code>config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize; config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; </code></pre> <p>The data shape will be changed after applying this setting.</p> <pre><code>[ { "$id":"1", "Category":{ "$id":"2", "Products":[ { "$id":"3", "Category":{ "$ref":"2" }, "Id":2, "Name":"Yogurt" }, { "$ref":"1" } ], "Id":1, "Name":"Diary" }, "Id":1, "Name":"Whole Milk" }, { "$ref":"3" } ] </code></pre> <p>The $id and $ref keeps the all the references and makes the object graph level flat, but the client code needs to know the shape change to consume the data and it only applies to JSON.NET serializer as well.</p> <h1>Fix 3: Ignore and preserve reference attributes</h1> <p>This fix is decorate attributes on model class to control the serialization behavior on model or property level. To ignore the property:</p> <pre><code>1: public class Category 2: { 3: public int Id { get; set; } 4: public string Name { get; set; } 5: 6: [JsonIgnore] 7: [IgnoreDataMember] 8: public virtual ICollection&lt;Product&gt; Products { get; set; } 9: } </code></pre> <p>JsonIgnore is for JSON.NET and IgnoreDataMember is for XmlDCSerializer. To preserve reference:</p> <pre><code>1: // Fix 3 2: [JsonObject(IsReference = true)] 3: public class Category 4: { 5: public int Id { get; set; } 6: public string Name { get; set; } 7: 8: // Fix 3 9: //[JsonIgnore] 10: //[IgnoreDataMember] 11: public virtual ICollection&lt;Product&gt; Products { get; set; } 12: } 13: 14: [DataContract(IsReference = true)] 15: public class Product 16: { 17: [Key] 18: public int Id { get; set; } 19: 20: [DataMember] 21: public string Name { get; set; } 22: 23: [DataMember] 24: public virtual Category Category { get; set; } 25: } </code></pre> <p><code>JsonObject(IsReference = true)]</code>is for JSON.NET and <code>[DataContract(IsReference = true)]</code> is for XmlDCSerializer. Note that: after applying <code>DataContract</code> on class, you need to add <code>DataMember</code> to properties that you want to serialize.</p> <p>The attributes can be applied on both json and xml serializer and gives more controls on model class.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload