I have a situation where I would like to create a 1-to-1 relationship in Entity Framework (4.2) using code first fluent configuration API, the twist is that I need to use a link/map table to do it.
Here is a diagram of my table layout.
This is what I am after ...
HasOptional(x => x.FieldPerson)
.WithOptionalPrincipal()
.Map(map =>
{
map.ToTable("user_account__field_person");
map.MapLeftKey("user_account_id");
map.MapRightKey("field_person_id");
});
... but there doesn't seem to be a way to do it.
The alternative is to create my 3 entities and manage them myself using joins where appropriate.
Is this even possible or is it a scenario that EF fluent API doesn't handle?
I'm going to answer my own question again!
I don't think EF code first deals with this situation because the whole concept of a link/map table suggests a many-to-many relationship.
For my particular case this is a database-first situation and I "have" to deal with these tables as I cannot change the database as this stage.
What I've done is use a Many-to-Many code first configuration ...
HasMany(t=>t.FieldPersons)
.WithMany(t=>t.UserAccounts)
.Map(map =>
{
map.ToTable("user_account__field_person");
map.MapLeftKey("user_account_id");
map.MapRightKey("field_person_id");
});
... as there is nothing in my database schema to prevent me from creating a many to many relationship. If I did want that behaviour then I would simply need to add a unique constraint on each field (which EF code first doesn't support anyway)