asp.net-mvc-3 - 我应该如何在 MVC3 中使用 Code First Entity Framework (4.1) 声明外键关系?

标签 asp.net-mvc-3 ef-code-first entity-framework-4.1

我一直在寻找有关如何使用代码优先 EF 4.1 声明外键关系和其他约束的资源,但运气不佳。基本上我是在代码中构建数据模型并使用 MVC3 来查询该模型。一切都通过 MVC 运行,这很棒(感谢 Microsoft!),但现在我希望它不起作用,因为我需要数据模型约束。

例如,我有一个 Order 对象,它具有大量属于外部对象(表)的属性。现在我可以毫无问题地创建订单,但无法添加外键或外部对象。 MVC3设置这个没问题。

我意识到我可以在保存之前自己将对象添加到 Controller 类中,但如果未满足约束关系,我希望对 DbContext.SaveChanges() 的调用失败。

NEW INFORMATION

So, specifically, I would like an exception to occur when I attempt to save an Order object without specifying a customer object. This does not seem to be the behavior if I just compose the objects as described in most Code First EF documentation.

最新代码:

public class Order
{
    public int Id { get; set; }

    [ForeignKey( "Parent" )]
    public Patient Patient { get; set; }

    [ForeignKey("CertificationPeriod")]
    public CertificationPeriod CertificationPeriod { get; set; }

    [ForeignKey("Agency")]
    public Agency Agency { get; set; }

    [ForeignKey("Diagnosis")]
    public Diagnosis PrimaryDiagnosis { get; set; }

    [ForeignKey("OrderApprovalStatus")]
    public OrderApprovalStatus ApprovalStatus { get; set; }

    [ForeignKey("User")]
    public User User { get; set; }

    [ForeignKey("User")]
    public User Submitter { get; set; }

    public DateTime ApprovalDate { get; set; }
    public DateTime SubmittedDate { get; set; }
    public Boolean IsDeprecated { get; set; }
}

这是我在访问 VS 为患者生成的 View 时遇到的错误:

ERROR MESSAGE

The ForeignKeyAttribute on property 'Patient' on type 'PhysicianPortal.Models.Order' is not valid. The foreign key name 'Parent' was not found on the dependent type 'PhysicianPortal.Models.Order'. The Name value should be a comma separated list of foreign key property names.

问候,

吉多

最佳答案

如果您有一个 Order 类,添加一个引用模型中另一个类的属性(例如 Customer)应该足以让 EF 知道其中存在关系:

public class Order
{
    public int ID { get; set; }

    // Some other properties

    // Foreign key to customer
    public virtual Customer Customer { get; set; }
}

您始终可以显式设置 FK 关系:

public class Order
{
    public int ID { get; set; }

    // Some other properties

    // Foreign key to customer
    [ForeignKey("Customer")]
    public string CustomerID { get; set; }
    public virtual Customer Customer { get; set; }
}

ForeignKeyAttribute 构造函数采用字符串作为参数:如果将其放置在外键属性上,它表示关联导航属性的名称。如果将其放置在导航属性上,它表示关联外键的名称。

这意味着,如果您将 ForeignKeyAttribute 放置在 Customer 属性上,则该属性将在构造函数中采用 CustomerID:

public string CustomerID { get; set; }
[ForeignKey("CustomerID")]
public virtual Customer Customer { get; set; }
<小时/>

根据最新代码进行编辑 由于这一行,您会收到该错误:

[ForeignKey("Parent")]
public Patient Patient { get; set; }

EF 将查找名为 Parent 的属性以将其用作外键强制执行器。你可以做两件事:

1) 删除 ForeignKeyAttribute 并将其替换为 RequiredAttribute 以将关系标记为必需:

[Required]
public virtual Patient Patient { get; set; }

使用 RequiredAttribute 修饰属性还有一个很好的副作用:数据库中的关系是使用 ON DELETE CASCADE 创建的。

我还建议将属性设为虚拟以启用延迟加载。

2) 创建一个名为 Parent 的属性,该属性将用作外键。在这种情况下,调用它例如 ParentID 可能更有意义(您还需要更改 ForeignKeyAttribute 中的名称):

public int ParentID { get; set; }

根据我在这种情况下的经验,反之亦然效果更好:

[ForeignKey("Patient")]
public int ParentID { get; set; }

public virtual Patient Patient { get; set; }

关于asp.net-mvc-3 - 我应该如何在 MVC3 中使用 Code First Entity Framework (4.1) 声明外键关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5542864/

相关文章:

entity-framework - 重命名 Entity Framework 为代码优先开发生成的类

entity-framework - Entity Framework 更新多对多关系 - POCO

.net - 在 ASP.NET MVC 中添加对 View 的引用

c# - 使用 Razor 在 MVC 3 中动态禁用表单字段

c# - 如何更新 Entity Framework 7 迁移和数据库 - Code first

entity-framework - 如何在远程目标上初始化 EF Code First 迁移

c# - 如何在 EntityTypeConfiguration 类中设置外键

.net - Entity Framework 4.1 对 TPT 和 TPCT 模型进行了更新?

c# - View 向 Controller Action 返回 NULL(模型未绑定(bind))

jquery - 使用附加字段对模型集合进行远程验证