0

I have two tables, MenuItem and SearchTerm. I have a foreign key on SearchTerm that is linked to the primary key on MenuItem.

I'm using the database-first approach with entity framework.

The result of the above is that the models generated will contain references to one another. This allows me to get all the data I need in a single statement

//Where() condition omitted for this example.
IList<MenuItem> menuItems = db.MenuItems.Include("SearchTerm").ToList();

This brings back the data I need. The issue I am having is that when trying to return this data through an ajax call, I get an error Self referencing loop detected in my response. I understand this happens because it is trying to serialize the json and is unable to do so.

Is there a way I can fetch the data so that it will only include the first two levels (MenuItem, MenuItem.SearchTerms), or is this something I should be setting on my ajax request?

Jack Zach Tibbles
  • 800
  • 1
  • 11
  • 27

1 Answers1

0

There are at least 2 options to solve this problem.

  1. Control json serialization and serialize only required amount of levels Proper JSON serialization in MVC 4
  2. Disable lazy loading on dbContext Turn off lazy loading for all entities
Community
  • 1
  • 1
Vladimir Sachek
  • 1,126
  • 1
  • 7
  • 20