0

Environment:

  • .net 4.6 1.0.0-rc1-update2
  • Entity Framework core rc final.

My API controller is throwing this exception:

An unhandled exception was thrown by the application. Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'Site' with type 'Blog.Model.Site'. Path '[0].Menus[0]'.

How can I implement the fixes suggested here: JSON.Net Self referencing loop detected

As far as I know, EF core does not implement lazy loading or proxy creation.

My controller simply returns the collection generated by this query:

public async Task<List<Site>> GetActiveSites()
{
    var query = db.Sites.Where(x => x.Active)
        .Include(x => x.Menus)
        .ThenInclude(m => m.MenuContentItems)
        .ThenInclude(x => x.ContentItem);

    return await query.ToListAsync();
}

Where:

  • Site(1) - Menu(*)
  • Menu(1) - MenuontentItem(*)
  • MenuContentItem(*) - ContentItem(1)
Community
  • 1
  • 1
  • http://stackoverflow.com/questions/17313632/self-referencing-loop-detected-getting-back-data-from-webapi-to-the-browser See the answer provided by Stewart Hou. –  May 18 '16 at 18:43
  • did you find an actual solution for this? – czioutas Nov 22 '16 at 14:15
  • @Drakoumel Yes, see link in comment above yours. –  Nov 22 '16 at 17:06
  • You might have a look at my answer on **[“Self Referencing Loop Detected” exception with JSON.Net](https://stackoverflow.com/questions/40472419/self-referencing-loop-detected-exception-with-json-net/51235783#51235783)** page. – Murat Yıldız Jul 08 '18 at 20:41

1 Answers1

0

Create a view model without the bi-directional mapping that EF has. Populate that view model from your your ef query.

Fran
  • 6,440
  • 1
  • 23
  • 35