实体框架 CTP 5 一对一映射

发布于 2024-10-19 17:58:26 字数 662 浏览 4 评论 0原文

我有两个表:

Requirement

RequirementId - PK

Fixture

FixtureId - PK

RequirementId - FK / NULLABLE / Unique Constraint

一个夹具只能有 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; }
}

也许我的理解都是错误的,所以任何建议都会很好。 提前致谢

I have two tables:

Requirement

RequirementId - PK

Fixture

FixtureId - PK

RequirementId - FK / NULLABLE / Unique Constraint

A Fixture can only have 1 Requirement, no other Fixture should be able to reference the same Requirement. It is not mandatory for a Fixture to have a Requirement, its optional.

What i have done is, in Sql Server i have placed a Unique constraint on the RequirementId column in the Fixture table. How do I setup the mapping for this in Entity Framework CTP 5 ?

Also would it be possible to have a bidirectional navigation property on each entity?

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

Maybe im getting this all wrong, so any advice would be great.
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2024-10-26 17:58:26

您所追求的称为“一对一外键关联”,并且像 Ladislav 提到的那样,EF 本身并不支持。但是,我在 本文

What you are after is called One-to-One Foreign Key Associations and like Ladislav mentioned is not natively supported by EF. However, I showed how to implement it with Code First in this article.

溺孤伤于心 2024-10-26 17:58:26

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

Unfortunatelly current version of EF is not able to work with unique constraint. The only way how to achieve 1:0..1 mapping in EF is "sharing PK". It means that if main entity is Fixture it will have FixtureId as its PK and Requirement will have FixtureId as its PK and FK to Fixture.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文