EF 4.1 - Code First - JSON Circular Reference Serialization Error
Solution 1
You could try to remove the virtual
keyword from all navigation properties to disable lazy loading and proxy creation and then use eager loading instead to load the required object graph explicitely:
public ActionResult GetAll()
{
return Json(ppEFContext.Orders
.Include(o => o.Patient)
.Include(o => o.Patient.PatientAddress)
.Include(o => o.CertificationPeriod)
.Include(o => o.Agency)
.Include(o => o.Agency.Address)
.Include(o => o.PrimaryDiagnosis)
.Include(o => o.ApprovalStatus)
.Include(o => o.Approver)
.Include(o => o.Submitter),
JsonRequestBehavior.AllowGet);
}
Referring to your previous post it looks like your application isn't relying on lazy loading anyway because you introduced there the virtual properties to load the object graph lazily, possibly causing now the serialization trouble.
Edit
It's not necessary to remove the virtual
keyword from the navigation properties (which would make lazy loading completely impossible for the model). It's enough to disable proxy creation (which disables lazy loading as well) for the specific circumstances where proxies are disturbing, like serialization:
ppEFContext.Configuration.ProxyCreationEnabled = false;
This disables proxy creation only for the specific context instance ppEFContext
.
(I've just seen, @WillC already mentioned it here. Upvote for this edit please to his answer.)
Solution 2
When you know that you need to serialize from a particular context, you can disable proxy creation for that particular query like below. This has worked for me and is better than revising my model classes.
using (var context = new MeContext())
{
context.Configuration.ProxyCreationEnabled = false;
return context.cars.Where(w => w.Brand == "Ferrari")
}
This approach takes away the proxy object type for this particular instance of the context so the returned objects are the actual class and therefore serialization is not a problem.
ie:
{Models.car}
instead of
{System.Data.Entity.DynamicProxies.car_231710A36F27E54BC6CE99BB50E0FE3B6BD4462ECA19695CD1BABB79605296EB}
Solution 3
The problem is that your are actually serializing an entity framework generated proxy object. Unfortunatly this has some issues when used with the JSON serializer. You might consider to map your entities to special simple POCO classes for the sake of JSON compatibility.
Solution 4
There is an attribute to add to Entity Framework objects
[ScriptIgnore]
This makes the code not perform Circular references.
Solution 5
I think they have fixed this in the latest version.
Check out the help docs under the section "Serializing and Deserializing JSON -> Serialization and Preserving Object References".
Set this setting when initializing the JSON.Net Serializer:
PreserveReferencesHandling = PreserveReferencesHandling.Objects;
So an example would be this:
var serializerSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects };
string json = JsonConvert.SerializeObject(people, Formatting.Indented, serializerSettings);
I verified that this works with my code first solution, and a circular reference in the navigation properties. If you look at the resulting JSON it should have "$id" and "$ref" properties everywhere.
Related videos on Youtube
Guido Anselmi
Updated on January 30, 2020Comments
-
Guido Anselmi about 4 years
I am getting an a Circular Reference Serialization Error although, to my knowledge I do not have any circular references. I am retrieving a set of Orders from the database and sending them to the client as JSON. All the code is shown below.
This is the error:
Error
A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Order_83CECF2AA4DE38232F9077D4B26941AB96BC61230419EA8AC42C9100E6072812'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Order_83CECF2AA4DE38232F9077D4B26941AB96BC61230419EA8AC42C9100E6072812'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
My classes are as follows:
Order
public class Order { [Key] public int OrderId { get; set; } public int PatientId { get; set; } public virtual Patient Patient { get; set; } public int CertificationPeriodId { get; set; } public virtual CertificationPeriod CertificationPeriod { get; set; } public int AgencyId { get; set; } public virtual Agency Agency { get; set; } public int PrimaryDiagnosisId { get; set; } public virtual Diagnosis PrimaryDiagnosis { get; set; } public int ApprovalStatusId { get; set; } public virtual OrderApprovalStatus ApprovalStatus { get; set; } public int ApproverId { get; set; } public virtual User Approver { get; set; } public int SubmitterId { get; set; } public virtual User Submitter { get; set; } public DateTime ApprovalDate { get; set; } public DateTime SubmittedDate { get; set; } public Boolean IsDeprecated { get; set; } }
Patient
public class Patient { [Key] public int PatientId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string MiddleInitial { get; set; } public bool IsMale; public DateTime DateOfBirth { get; set; } public int PatientAddressId { get; set; } public Address PatientAddress { get; set; } public bool IsDeprecated { get; set; } }
Certification Period
public class CertificationPeriod { [Key] public int CertificationPeriodId { get; set; } public DateTime startDate { get; set; } public DateTime endDate { get; set; } public bool isDeprecated { get; set; } }
Agency
public class Agency { [Key] public int AgencyId { get; set; } public string Name { get; set; } public int PatientAddressId { get; set; } public virtual Address Address { get; set; } }
Diagnosis
public class Diagnosis { [Key] public int DiagnosisId { get; set; } public string Icd9Code { get; set; } public string Description { get; set; } public DateTime DateOfDiagnosis { get; set; } public string Onset { get; set; } public string Details { get; set; } }
OrderApprovalStatus
public class OrderApprovalStatus { [Key] public int OrderApprovalStatusId { get; set; } public string Status { get; set; } }
User
public class User { [Key] public int UserId { get; set; } public string Login { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string NPI { get; set; } public string Email { get; set; } }
NOTE: ADDRESS CLASS IS NEW ADDITION DURING EDIT
Address
public class Address { [Key] public int AddressId { get; set; } public string StreetAddress { get; set; } public string City { get; set; } public string State { get; set; } public string Zip { get; set; } public string Phone { get; set; } public string Title { get; set; } public string Label { get; set; } }
The code that executes the serialization is here:
Excerpt from OrderController
public ActionResult GetAll() { return Json(ppEFContext.Orders, JsonRequestBehavior.AllowGet); }
Thanks
-
Ladislav Mrnka about 13 yearsAnd what about
Address
entity? -
Guido Anselmi about 13 years@Ladislav - Will add now
-
Paul Sasik over 12 years@Guido: No need to add a sig in your question. SO autosigns all posts with SO flair.
-
-
Guido Anselmi about 13 years@Slauma: Thanks! That worked. What exactly is happening with all of those .Include statements?
-
Slauma about 13 yearsIt causes to load all the related entities when the query is executed. You can find a little tutorial about the different options to load related entities here: blogs.msdn.com/b/adonet/archive/2011/01/31/…. It is part of this series on EF 4.1: blogs.msdn.com/b/adonet/archive/2011/01/27/…
-
Guido Anselmi about 13 years@Slauma - Thanks for the links.
-
Yuck almost 13 yearsWas the
Include()
method removed from the 4.1 release? The version I have wants astring
as parameter. -
Slauma almost 13 years@Yuck: No, it's not removed. You need a reference to
EntityFramework.dll
assembly in your project andusing System.Data.Entity;
as namespace, then you should have the overload with the lambda expression. -
Yuck almost 13 yearsThank you. I hate extension methods for this reason.
-
John B about 12 yearsWrote a blog post on the subject: Wrote a blog post on the subject: johnnycode.com/blog/2012/04/10/…
-
Ricardo Sanchez almost 12 yearsThis is correct, you don't need to change your EF entities, just map them to an entity that will only be used by the View.
-
rafasoares over 11 yearsThis is an elegant enough solution for most cases.
-
Jimmy Bosse over 11 yearsThis did not prevent my circular reference problem, BUT is did allow me to remove attributes from the serialization that I didn't want to return in my API. Thanks!
-
Shelby115 over 8 yearsLink is dead. This is why it is frowned upon to do link-only answers. You should always quote/re-iterate the important parts of the link.
-
nzsai over 4 yearsUseful suggestion. I am setting navigational properties (that are cyclic) to null before setting the return value of the ASP.Net Web Api method.