Articles in this section
Category / Section

How to fix 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.

 

Error Message

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.

 

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.

 

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.

                Properties Window

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 model

Result using view model/ DTO.

 

 

Conclusion

I hope you enjoyed learning about how Circular reference errors in ASP.NET MVC Grid.

You can refer to our ASP.NET MVC Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Grid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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