How to get a complete object using Code First Entity Framework 4.1

I am trying to return a fully deep object (with all foreign key relationships) JSON, but I get zeros for all the referenced objects.

Here is the call to get the object:

public ActionResult GetAll() { return Json(ppEFContext.Orders, JsonRequestBehavior.AllowGet); } 

And here is the Order object itself:

 public class Order { public int Id { get; set; } public Patient Patient { get; set; } public CertificationPeriod CertificationPeriod { get; set; } public Agency Agency { get; set; } public Diagnosis PrimaryDiagnosis { get; set; } public OrderApprovalStatus ApprovalStatus { get; set; } public User Approver { get; set; } public User Submitter { get; set; } public DateTime ApprovalDate { get; set; } public DateTime SubmittedDate { get; set; } public Boolean IsDeprecated { get; set; } } 

I have not yet found a good resource for using EF 4.1 annotations. If you could offer a good, that is, an answer, you could give me a link, and that would be enough for an answer!

Hello,

Guido

Update

I added a virtual keyword according to Saxman, and now I am dealing with a circular link error.

+4
c # ef-code-first code-first
Apr 07 '11 at 17:50
source share
2 answers

Add the virtual in front of your related objects:

 public class Order { public int Id { get; set; } public virtual Patient Patient { get; set; } public virtual CertificationPeriod CertificationPeriod { get; set; } public virtual Agency Agency { get; set; } public virtual Diagnosis PrimaryDiagnosis { get; set; } public virtual OrderApprovalStatus ApprovalStatus { get; set; } public virtual User Approver { get; set; } public virtual User Submitter { get; set; } public DateTime ApprovalDate { get; set; } public DateTime SubmittedDate { get; set; } public Boolean IsDeprecated { get; set; } } 

As a result, an error A circular reference was detected while serializing an object... may occur if your objects have links to each other. In this case, you will need to create a ViewModel or something similar to overcome this problem. Or use LINQ to create an anonymous object.

+2
Apr 7 '11 at 18:27
source share
+2
Apr 7 '11 at 18:10
source share



All Articles