We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Circular reference errors from UrlDatasource Json serializer

Hi guys,

I am having circular reference problems when trying to get table data from related tables. There is a M:N relationship between aspnetusers and aspnetroles. Join table apnetuserroles is not mapped by EF6, so I am using this code to get data, and results are ok:
IEnumerable DataSource = db.aspnetusers.Where(c => c.aspnetroles.Any()).ToList<aspnetusers>();

But some circular reference errors ocurrs when trying to serialize data for returning json results:
return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);

1 - Is there an option to avoid this errors?

2 - I've found some workarounds for this problem. So I tried the following using Newtonsoft json serializer:
var list = JsonConvert.SerializeObject(DataSource, Formatting.None, new JsonSerializerSettings()
            {
                    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
            });
This code serializes data without errors. Is it possible to send its results to grid?

Thank you! 

5 Replies

MS Madhu Sudhanan P Syncfusion Team November 2, 2018 09:41 AM UTC

Hi Vanderlei, 

Thanks for contacting Syncfusion support. 

Query : Circular reference errors from UrlDatasource Json serializer 
 
When serializing multiple entities there might be changes for circular reference exception and hence we suggest you to use the ViewModel instead of ReferenceLoopingHandling to resolve the issue. 

 
Kindly refer to the below code example for more information. In the below code example we have created viewModel class and returned the ViewModel list instead of the original entity. 
 
 
@Html.EJS().Grid("Grid").DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").Adaptor("UrlAdaptor"); }).Columns(col => 
       { 
           col.Field("AddressType").HeaderText("AddressType").Width("120").Add(); 
           col.Field("AddressLine1").HeaderText("AddressLine1").Width("120").Add(); 
           col.Field("Title").HeaderText("Title").Width("120").Add(); 
 
       }).AllowPaging().AllowFiltering().Render() 

// you can bind the data that you want to show in Grid    
public class CustomerAddressVM  
    { 
        public string AddressType { get; set; } 
 
        public string AddressLine1 { get; set; } 
 
        public string Title { get; set; } 
    } 

using EntityExampleWithRelationships.ViewModels; 
 
. . . . . 
public ActionResult UrlDatasource(DataManagerRequest dm) { 
 
//Original entity collection 
var customerAddresses = db.CustomerAddresses.Include(c => c.Address).Include(c => c.Customer); 
 
//Viewmodel entity collection 
            List<CustomerAddressVM> customer = new List<CustomerAddressVM>(); 
            foreach (var item in customerAddresses) 
 
            { 
 
                CustomerAddressVM objcvm = new CustomerAddressVM(); // ViewModel 
 
                objcvm.Title = item.Customer.Title; 
 
                objcvm.AddressLine1 = item.Address.AddressLine1; 
 
                objcvm.AddressType = item.AddressType; 
 
                customer.Add(objcvm); 
 
            } 
return dm.RequiresCounts ? Json(new { result = customer.ToList(), count = count }) : Json(customer.ToList()); 


Regards, 
Madhu Sudhanan P 



VA Vanderlei November 2, 2018 02:22 PM UTC

Hi Madhu, thanks for your reply.

Will I be able to CRUD by using that approach? I mean, creating a viewmodel for aspnetusers and aspnetroles and saving its changes, will PKs in join table aspnetuserroles (M:N relationship table not mappped in EF6) be updated/inserted?

Many thanks!


MS Madhu Sudhanan P Syncfusion Team November 5, 2018 12:53 PM UTC

Hi Vanderlei, 

Query : Will I be able to CRUD by using that approach?  
 
Yes, you can able to CRUD operation using the below approach.  In the below code example, We have got the updated record in CustomerAddressVM object and remapped to the original entity which then can be used to update to the db. 

         
        public ActionResult Update(CustomerAddressVM value) 
        { 
            var customer = db.CustomerAddresses.Where(c => c.CustomerID === customerModel.CustomerID); 
 
            customer.Customer.Title = customerModel.Title; 
 
            customer.Address.AddressLine1 = customerModel.AddressLine1; 
 
            customer.AddressType = customerModel.AddressType; 
             
            // Use the customer class to update the records 
            return Json(value, JsonRequestBehavior.AllowGet); 
        } 
 

Regards, 
Madhu Sudhanan P 



VA Vanderlei November 5, 2018 01:11 PM UTC

Ok Madhu,

Thanks!


MS Madhu Sudhanan P Syncfusion Team November 7, 2018 04:56 AM UTC

Hi Vanderlei, 
We are glad that your requirement has been achieved. 
Regards, 
Madhu Sudhanan P 


Loader.
Live Chat Icon For mobile
Up arrow icon