开发者

Entity Framework CTP 5 One to One mapping

开发者 https://www.devze.com 2023-02-14 05:45 出处:网络
I have two tables: Requirement RequirementId - PK Fixture FixtureId - PK RequirementId - FK / NULLABLE / Unique Constraint

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 col开发者_开发百科umn 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消