Archive for the ‘JSON’ Category

The “Circular Reference” issue with Entity Framework, JSON and retaining relationship data

June 25th, 2017 Michael Bell No comments

I’ve got a WCF service that I was trying to return multiple data sets in the same call. You can read about that in the previous post. What I tried today for the first time, was to return objects along with other objects that shared a relationship. What I found was that you get circular reference issues when you try to do this. After about a day of googling, I found that you can use the library to get around that issue. The example code follows:

[WebInvoke(Method = "POST",
 BodyStyle = WebMessageBodyStyle.WrappedRequest,
 ResponseFormat = WebMessageFormat.Json)]
 public dynamic getGalleryOutput(int GalleryID)
 EasyGalleryDataContext data = new EasyGalleryDataContext();

 Folder folder = getAlbum(GalleryID);

 var includes = from gti in data.GalleryTemplateIncludes
 select new {

 JsonSerializerSettings jsSettings = new JsonSerializerSettings();
 jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

 dynamic[] results = { folder, folder.Images, folder.GalleryTemplate, includes };
 return JsonConvert.SerializeObject(results, Formatting.None, jsSettings);
Categories: AJAX, ASP.NET, Entity Framework, JSON, WCF Tags: