4

I am trying to return as JSON the fully deep object (with all of the foreign key relationships filled in) but I am getting nulls 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 on using EF 4.1 Annotations. If you could suggest a good one, that has the answer, you could give me the link and that would be enough of an answer for me!

Regards,

Guido

Update

I added the virtual keyword as per Saxman and am now dealing with the circular reference error issue.

Guido Anselmi
  • 3,872
  • 8
  • 35
  • 58
  • Have a look here: http://stackoverflow.com/questions/4251794/how-to-have-many-to-many-association-in-entity-framework-code-first what type of an Enumerable is the OrderApprovalStatus etc. – TBohnen.jnr Apr 07 '11 at 18:07

2 Answers2

3

Add the virtual keyword before your related entities:

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; }
}

You might end up with a A circular reference was detected while serializing an object... error if your objects have references of each other. In that case, you will need to create a ViewModel or something similar to overcome this problem. Or use LINQ to project an anonymous object.

Saxman
  • 5,009
  • 11
  • 51
  • 72
  • This made progress, but now I am where you describe. With a circular reference exception. I am reading about adding the id of the foreign object to the model and other solutions as well. – Guido Anselmi Apr 07 '11 at 19:00
  • You can't really do much about it at the moment, serializing with an object referencing another object is not possible. Like I've suggested, you could create a ViewModel that only have the data you needed, or project to an anonymous object, i.e: http://stackoverflow.com/questions/4436328/how-do-i-avoid-a-circular-reference-while-serializing-entity-framework-class – Saxman Apr 07 '11 at 21:47
2

Read about Loading Related Objects

archil
  • 39,013
  • 7
  • 65
  • 82
  • My first thought was to look at lazy loading. – Andrew Charlton Apr 07 '11 at 19:12
  • Yes, but if you always need to load every referenced property, explicitly writing Include with every load isn't comfortable, maybe there's another way to configure lazy loading – archil Apr 07 '11 at 19:45