c# - 如何在 EF 4.1 Code First 中建立外键关系

标签 c# entity-framework code-first

所以我整个上午都在为这个问题苦苦挣扎。我已经阅读了几篇文章,我的工作大致基于这篇文章:

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

这是我当前的错误:

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

这是类:

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

我假设我已经使用“Fluent API”做了一些事情。不幸的是,我对 Fluent API 并不流利,所以我想验证这确实是缺失的部分。

谢谢,

吉多

最佳答案

您需要使用 ForeignKeyAttribute装饰您的外键属性。

关于c# - 如何在 EF 4.1 Code First 中建立外键关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5587088/

相关文章:

entity-framework - 如何禁用代码优先迁移

c# - 首先了解 Entity Framework 代码中的 ForeignKey 属性

c# - 您可以在 C# 中实例化泛型类型参数的嵌套类型吗?

entity-framework - MVC3和代码优先迁移-自创建数据库以来 “model backing the '等上下文已更改”

c# - 你如何模拟 - _context.Entry(myEntity).Property(e => e.myProperty).IsModified

entity-framework - Entity Framework 更新失败

entity-framework - 没有外键的 Entity Framework 代码优先映射

c# - HTTP POST XML 到 REST 单例服务抛出 NotSupportedException

c# - 选择 KeyValuePair 列表的值

c# - `[FromQuery]` IEnumerable<SomeObject> 在 ASP.NET Core 3.1 中解析?