简体   繁体   中英

EF6: How to avoid circular reference?

What are the possibilities to avoid circular reference with Entity Framework 6 during JSON serialization with ASP.NET Web API?

I generated a edmx (Entity Data Model) file for Entity Framework 6, database first. I try to build an API with ASP.NET Web API. When I try to return my JSON object in my controllers I get a runtime exception of serialization because of circular reference.

Indeed when I double check my database and my entities I see one of my entity contains a list another entity that contain a list of my previous entity. let say I have a book entity that contains authors and each author entity contains a list of books. This is something common with relative database but impossible to resolve in JSON serialization (or impossible to resolve for the .NET serializer).

I don't want to change my database but I'm ready to break the wrong list into my entities or edmx file. What can I do?

What I have tried:

I already tried the solution that consist of creating new models or entities and using a mapping tool ( http://www.codeproject.com/Articles/292970/Avoiding-Circular-Reference-for-Entity-in-JSON-Ser or the solution explained by by Shawn Wildermuth on Pluralsight).

This solution sounds more like a workaround than a real solution. It should exist something in edmx file or in Entity Framework to tell the JSON serializer what can cause circular reference, what can and must be serialized and what cannot be serialized, right?

There is technically no problem to serialize the domain model directly. To avoid circular reference you cannot use lazy loading. You must keep control of the loading. To do so

  1. remove the virtual before each collection of your model (in code first approach)
  2. set lazy loading configuration to false (in database first approach)

Don't try to serialize your domain model directly. Create a view model that returns the data in the exact format you want. Use you domain model to populate the view model. Much more information here Why do we use ViewModels?

You need to have a ViewModel that will act as the interface between your UI and your backend data structure. Backend data structure is designed to be easilly stored and retrieved to and from the database. So by writing some ViewModel "adapters" you are not violating the "don't repeat yourself" rule.

Hope it helps.

I had to solve the same problem today. The easiest and the clear way for me was to remove the Navigation property from one of the entities on the entity model. Open your model.edmx and remove the unwanted Navigation property which is causing the circular reference. In your case

Book references Author. so book entity model has Author navigation property.

If you do not want Author to reference Book, just remove Book navigation property from Author data model on the edmx file. So, there is only one way referencing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM