如何使用代码优先实体框架获取完整的对象4.1 [英] How to Get Full Object with Code First Entity Framework 4.1
问题描述
我正在尝试以JSON的形式返回完全深入的对象(填写所有外键关系) >以下是获取对象的调用:
public ActionResult GetAll()
{
return Json (ppEFContext.Orders,JsonRequestBehavior.AllowGet);
}
这里是Order对象本身:
public class Order
{
public int Id {get;组; }
public Patient Patient {get;组; }
public CertificationPeriod CertificationPeriod {get;组; }
public Agency Agency {get;组; }
公共诊断PrimaryDiagnosis {get;组; }
public OrderApprovalStatus ApprovalStatus {get;组; }
public User Approver {get;组; }
public User Submitter {get;组; }
public DateTime ApprovalDate {get;组; }
public DateTime SubmittedDate {get;组; }
public Boolean IsDeprecated {get;组;
}
我还没有找到使用EF 4.1注释的好资源。如果你能建议一个好的,那就有答案,你可以给我链接,这对我来说足够了。
问候,
Guido
更新
我根据Saxman添加了虚拟关键字,现在
处理循环引用
错误问题。
在相关实体之前添加 virtual
关键字:
public class Order
{
public int Id {get;组; }
public virtual患者病人{get;组; }
public virtual CertificationPeriod CertificationPeriod {get;组; }
public virtual Agency Agency {get;组; }
public virtual Diagnosis PrimaryDiagnosis {get;组; }
public virtual OrderApprovalStatus ApprovalStatus {get;组; }
public virtual User Approver {get;组; }
public virtual User Submitter {get;组; }
public DateTime ApprovalDate {get;组; }
public DateTime SubmittedDate {get;组; }
public Boolean IsDeprecated {get;组; }
}
你可能会得到一个一个循环引用如果您的对象具有彼此的引用,则会在序列化对象...
时检测到错误。在这种情况下,您将需要创建一个 ViewModel
或类似的东西来克服这个问题。或使用LINQ来投射匿名对象。
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.
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.
这篇关于如何使用代码优先实体框架获取完整的对象4.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!