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

A circular reference was detected while serializing an object of type

Hello,

I am learning MVC to develop a new project.

I'm looking at this example and modifying it with your controls:

https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

To summarize: I have these 3 classes

 public class Student
    {
        public int ID { get; set; }
        public string LastName { get; set; }
        public string FirstMidName { get; set; }
        public DateTime EnrollmentDate { get; set; }

        public virtual ICollection<Enrollment> Enrollments { get; set; }
    }

public enum Grade
    {
        A, B, C, D, F
    }

    public class Enrollment
    {
        public int EnrollmentID { get; set; }
        public int CourseID { get; set; }
        public int StudentID { get; set; }
        public Grade? Grade { get; set; }

        public virtual Course Course { get; set; }
        public virtual Student Student { get; set; }
    }

public class Course
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int CourseID { get; set; }
        public string Title { get; set; }
        public int Credits { get; set; }

        public virtual ICollection<Enrollment> Enrollments { get; set; }
    }

I want to show all Students in a grid:

View code:
@(Html.EJ().Grid<ContosoUniversity.Models.Student>("FlatGrid")
        .Datasource((IEnumerable<object>)ViewBag.datasource)
        .AllowSorting()
        .AllowPaging()
        .Columns(col =>
        {
            col.Field("LastName").HeaderText("LastName").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
            col.Field("FirstMidName").HeaderText("FirstMidName").Width(80).Add();
            col.Field("EnrollmentDate").HeaderText("EnrollmentDate").TextAlign(TextAlign.Right).Width(75).Add();
           
        }))

Controller code

 // GET: Student
        public ActionResult Index()
        {
            ViewBag.datasource = db.Students.ToList();
            return View();
        }

When I run, I get this error



If I modify the Student class

    public class Student
    {
        public int ID { get; set; }
        public string LastName { get; set; }
        public string FirstMidName { get; set; }
        public DateTime EnrollmentDate { get; set; }

        //public virtual ICollection<Enrollment> Enrollments { get; set; }
    }

The application runs perfectly

Thanks

6 Replies

AS Alan Sangeeth S Syncfusion Team July 3, 2015 06:55 AM UTC

Hi Manolo,


Thanks for using Syncfusion Products.


The Circular Reference error during serializing the object of the Entity data Model can be resolved in two ways.
  1. By using the ViewModel.
  2. Setting the ProxyCreationEnabled as false.


Please refer the below link in which a similar problem had been discussed.

http://juristr.com/blog/2011/08/javascriptserializer-circular-reference/

http://musingsandshouts.blogspot.in/2012/04/nopcommerce-and-ef-circular-reference.html

http://lou.prado.me/tech_blog/_net_entity_framework_error__exception_details__system_invalidoperationexception__a_circular_reference_was_detected_while_serializing_an_object_of_type_system_data_entity_dynamicproxies_client.php

Also please refer the following UG document for further reference.

http://help.syncfusion.com/ug/js/Documents/mvc.htm  

Please let us know if you have any queries.


Regards,
Alan Sangeeth S



MA Manolo July 8, 2015 07:33 AM UTC

Thanks.

I solve adding :

Configuration
.ProxyCreationEnabled = false;

in the conext constructor.



AS Alan Sangeeth S Syncfusion Team July 9, 2015 07:02 AM UTC

Hi Manolo,

Thanks for the update.

We are happy to hear that your issue has been resolved.

Please let us know if you need any further assistance. We will be happy to help you out.

Regards,
Alan Sangeeth S


DC Damian Chambers October 2, 2016 04:02 AM UTC

Where do you set Configuration.ProxyCreationEnabled = false;? Is that in the web.config file?


DC Damian Chambers October 2, 2016 04:23 AM UTC

No worries. Got it!


VA Venkatesh Ayothi Raman Syncfusion Team October 3, 2016 04:02 AM UTC

Hi Damian, 

Thanks for the update. 
We are happy to hear that your requirement is achieved. 

Thanks, 
Venkatesh Ayothiraman. 


Loader.
Live Chat Icon For mobile
Up arrow icon