Entity Framework one-to-one relationship mapping flattened in code

Posted by Josh Close on Stack Overflow See other posts from Stack Overflow or by Josh Close
Published on 2011-06-24T17:36:46Z Indexed on 2011/06/25 0:22 UTC
Read the original article Hit count: 182

I have a table structure like so.

Address:
AddressId int not null primary key identity
...more columns

AddressContinental:
AddressId int not null primary key identity foreign key to pk of Address
County
State

AddressInternational:
AddressId int not null primary key identity foreign key to pk of Address
ProvinceRegion

I don't have control over schema, this is just the way it is.

Now, what I want to do is have a single Address object.

public class Address
{
    public int AddressId { get; set; }
    public County County { get; set; }
    public State State { get; set }
    public ProvinceRegion { get; set; }
}

I want to have EF pull it out of the database as a single entity. When saving, I want to save the single entity and have EF know to split it into the three tables.

How would I map this in EF 4.1 Code First?

I've been searching around and haven't found anything that meets my case yet.

UPDATE

An address record will have a record in Address and one in either AddressContinental or AddressInternational, but not both.

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about entity-framework-4.1