Guido Anselmi
Guido Anselmi

Reputation: 3912

How to Establish Foreign Key Relationships in EF 4.1 Code First

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

Upvotes: 3

Views: 1966

Answers (4)

ckal
ckal

Reputation: 3570

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

Upvotes: 0

Guido Anselmi
Guido Anselmi

Reputation: 3912

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

Upvotes: 0

Youp Bernoulli
Youp Bernoulli

Reputation: 5655

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.

Upvotes: 0

Ken Pespisa
Ken Pespisa

Reputation: 22266

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

Upvotes: 5

Related Questions