5

我有两张桌子:

要求

RequirementId - PK

夹具

FixtureId - PK

RequirementId - FK / NULLABLE / 唯一约束

一个夹具只能有 1 个需求,其他夹具不能引用相同的需求。Fixture 有一个 Requirement 不是强制性的,它是可选的。

我所做的是,在 Sql Server 中,我在 Fixture 表中的 RequirementId 列上放置了唯一约束。如何在 Entity Framework CTP 5 中为此设置映射?

还有可能在每个实体上都有一个双向导航属性吗?

public class Fixture
{
    public int FixtureId { get; set; }
    public Requirement Requirement { get; set; }
}

public class Requirement
{
    public int RequirementId { get; set; }
    public Fixture Fixture { get; set; }
}

也许我把这一切都弄错了,所以任何建议都会很棒。提前致谢

4

2 回答 2

5

您所追求的称为一对一外键关联,就像 Ladislav 提到的那样,EF 本身并不支持。但是,我在本文中展示了如何使用 Code First 来实现它。

于 2011-03-02T20:50:17.687 回答
0

不幸的是,当前版本的 EF 无法使用唯一约束。EF中实现1:0..1映射的唯一方法就是“共享PK”。这意味着如果主要实体是 Fixture,它将具有 FixtureId 作为其 PK 并且 Requirement 将具有 FixtureId 作为其 PK 和 FK 到 Fixture。

于 2011-03-02T20:45:02.810 回答