【问题标题】:Entity Framework CTP 5 One to One mapping实体框架 CTP 5 一对一映射
【发布时间】:2011-07-07 14:05:24
【问题描述】:

我有两张桌子:

要求

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

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

【问题讨论】:

    标签: entity-framework entity-framework-4 entity-framework-ctp5 ef-code-first ef4-code-only


    【解决方案1】:

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

    【讨论】:

    • 太棒了。提前致谢
    【解决方案2】:

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

    【讨论】:

    • 如果需求必须能够在没有 Fixture 的情况下存在怎么办?
    猜你喜欢
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    相关资源
    最近更新 更多