3

So I have been struggling with this one all morning. I have read a few articles and I roughly basing my work off of this one:

http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx

This is my current error:

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Order_C00CE366506BD8C6592A3CF21B9D1C5921D31C03D7322A8F6E8EAD72E113EA95'.

Here is the class:

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

I am assuming that I have do something with the "Fluent API." I unfortunately am not fluent with the Fluent API and so I wanted to validate that this is in fact what is missing.

Thanks,

Guido

Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
Guido Anselmi
  • 3,872
  • 8
  • 35
  • 58
  • 2
    Are you using any type of serialization? WCF? That is most probably source of the exception. – Ladislav Mrnka Apr 07 '11 at 20:36
  • Do any of the child entities, such as OrderApprovalStatus, include backreferences to the parent Order entity? – jrista Apr 07 '11 at 20:36
  • 1
    @Ladislav - Yes I was serializing to JSON. That is where the error is occurring! Thanks I have had so many problems defining the DB it did not occur to me that I had fixed the DB but was now having problems with the serialization. – Guido Anselmi Apr 07 '11 at 21:35

4 Answers4

5

You'll need to use the ForeignKeyAttribute to decorate your foreign key properties.

Ken Pespisa
  • 21,989
  • 3
  • 55
  • 63
  • I thought this as well after scanning the article. But do you know if this caused the error. I don't think so. – Youp Bernoulli Apr 07 '11 at 20:24
  • I agree, it might not make the error go away. I was focused on answering the question in title, but you make a good point. Where is the circular reference coming from? – Ken Pespisa Apr 07 '11 at 20:27
  • I believe that ForeignKeyAttribute is needed only on `UserId` because other foreign keys should be handled by default convention. – Ladislav Mrnka Apr 07 '11 at 20:36
  • @ALL: I used to have the Foreign Key Attribute but it did not in fact make any difference. Actually I think it created a different error relating to metadata not being stored in the database. – Guido Anselmi Apr 07 '11 at 21:26
0

My first intuition gave me in [DataContract(IsReference=true)]. This will detect circular references and prevent them from being endlessly looped in object graphs. You might need to decorate one or more of the classes you define with this attribute and the named parameter IsReference. Take a google on DataContractAttribute and IsReference and read this post and the answer.

And about the establishment of foreign keys I suppose you just apply the ForeignKey attribute to the right fields / properties as @Ken already mentioned.

Community
  • 1
  • 1
Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
0

Ladislav answered this one in the comments:

Are you using any type of serialization? WCF? That is most probably source of the exception. – Ladislav Mrnka 1 hour ago

Guido Anselmi
  • 3,872
  • 8
  • 35
  • 58
0

Another option for getting around your circular reference, would be to disable proxy creation. You'll have to be more explicit with eagerly loading navigation properties, but it will stop the lazy load loop.

http://blogs.msdn.com/b/adonet/archive/2011/02/02/using-dbcontext-in-ef-feature-ctp5-part-8-working-with-proxies.aspx

ckal
  • 3,540
  • 1
  • 22
  • 21