Marius
Marius

Reputation: 9664

EF: how do i model 1:1 without db referential integrity?

Consider the following tables:

Base, Primary key: Id
Extension, Primary key: Id

The primary key in the "Extension" table has the same value as the primary key in the "Base" table. I want to model this in the edmx as such:

Base can have 0..1 (Zero or One) instances of Extension.

Extension can have 1 (One) instance of Base. Use Extension.Base to access the Base instance.

When I try to specify the mapping details for this association, I get an error such as this:

"Error 3021: Problem in mapping fragments starting at line xx:Each of the following columns in table Base is mapped to multiple conceptual side properties: Base.ID is mapped to "

How can I model a 1:0..1 relationship which is not backed by a database relationship? I basically want a relationship so I can query my read-model using Linq.

Upvotes: 0

Views: 550

Answers (2)

shawty
shawty

Reputation: 5829

If I'm reading you correct then your doing something similar to a problem I had to resolve to too long back in a messeging system.

Basically, I created an intermediate table, with 2 columns in, both ints, 1 held the id from one table, and the other the id from another table, that then allowed me to bridge the two together.

so EG:

--------------------------
| Base ID | Extension ID |
--------------------------

I did have to manage the table manually however, so a little extra work, but it did the trick.

Maybe not the exact schema you need, but I think the intermediate table whatever your scheme is a sensible way to look at addressing your needs.

Upvotes: 1

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13574

You should modify your mode classes like following for 1:1 relationship :-

Base Class

public class Base
{
    public int BaseID;
    public int ExtentionID;
    public virtual Extention Extension { get; set; }
}

Extention Class

public class Extention
{
    public int ExtentionID;
    public int BaseID;
    public Base Base { get; set; }
}

Upvotes: 0

Related Questions