Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework CTP 5 One to One mapping

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

like image 573
JCoder23 Avatar asked Mar 02 '11 20:03

JCoder23


1 Answers

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.

like image 172
Morteza Manavi Avatar answered Oct 03 '22 10:10

Morteza Manavi