Articles in this section
Category / Section

How to resolve circular reference error while using Entity Framework/Linq 2 SQL?

2 mins read

Problem

A circular reference error was detected while serializing an object of type XXXXX.

 

For example, when you try to access the MVC action as follows.

 

        public ActionResult GetData()
        {
 
            IEnumerable data = new NorthwindDataContext().Orders.ToList();
 
            return Json(data, JsonRequestBehavior.AllowGet);
        }

 

The request will sometimes get failed and the error messages will be thrown is as follows.

 

Response Circular reference error

Response Circular reference error

 

Cause

The circular reference error will occur when serializing the POCO entities which refers each other. For example consider the relationship between the entities Order and Customer. Here the Order and Customer relation are referenced each other.

 

Relationship between Order and Customer relation

Figure 1: Relationship between Order and Customer relation

 

When the JSON serializer tries to serialize the entity objectOrders with above relation it will try to serialize it`s relational object “Customers” and while traversing the “Customers” object it discovers circular reference and throws circular reference exception.

 

Solution

When using EF or LINQ to SQL, the circular reference error can be resolved using one of the following ways.

Setting the visibility of the Navigation property

 

If Entity Framework is used, then set the visibility of the navigation property as internal in the properties window as follows.

 

 Setting access as internal for navigation property

Entity Framework: Setting access as internal for navigation property

 

When using EF, you can also disable the proxy creation as follow to prevent proxy generation, which will avoid the circular reference error.

 

 
    public partial class NORTHWNDEntities : DbContext
    {
        public NORTHWNDEntities()
            : base("name=NORTHWNDEntities")
        {
            this.Configuration.ProxyCreationEnabled = false;
        }
       
        . . . . . . . 
    
    }
 

 

For LINQ to SQL, set the ChildProperty as false or set the Access as internal in the properties window of the particular relationship.

                Setting access as internal for navigation property

LINQ to SQL: Setting access as internal for navigation property

 

Using Data transfer object or View model

 

                This is the most recommended and safe way to avoid circular reference error, as it involves serializing only the required properties without exposing the POCO entity directly.

 

Other than avoiding circular reference error, it also helps in making the object light weight by excluding properties which are meant to seen by client which in turn reduces payload size while transferring.

 

The ViewModel/DTO can be used as follows.

 

        public class OrderView
        {
            public int OrderID { get; set; }
            public string CustomerID { get; set; }
            public int? EmployeeID { get; set; }
            public decimal? Freight { get; set; }
        }
 
        public ActionResult GetData()
        {
 
            IEnumerable data = new NorthwindDataContext().Orders
                .Select(o => new OrderView
                {
                    OrderID = o.OrderID,
                    CustomerID = o.CustomerID,
                    EmployeeID = o.EmployeeID,
                    Freight = o.Freight
                }).ToList();
 
            return Json(data, JsonRequestBehavior.AllowGet);
        }

 

Result using view mode

Result using view model/ DTO.

 

 


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied