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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

A circular reference was detected while serializing an object of type

Thread ID:

Created:

Updated:

Platform:

Replies:

119515 Jul 2,2015 02:33 PM UTC Oct 3,2016 04:02 AM UTC ASP.NET MVC 6
loading
Tags: Grid
Manolo
Asked On July 2, 2015 02:33 PM UTC

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

Alan Sangeeth S [Syncfusion]
Replied On 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


Manolo
Replied On July 8, 2015 07:33 AM UTC

Thanks.

I solve adding :

Configuration
.ProxyCreationEnabled = false;

in the conext constructor.


Alan Sangeeth S [Syncfusion]
Replied On 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

Damian Chambers
Replied On October 2, 2016 04:02 AM UTC

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

Damian Chambers
Replied On October 2, 2016 04:23 AM UTC

No worries. Got it!

Venkatesh Ayothi Raman [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;