How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

NathanAldenSr

Let's pretend I have three tables, each with a primary key called Id:

TableA
------
Id

TableB
------
Id

TableC
-----
Id

The intent is for each of these entities to have a one-to-one navigation property with each other so that I may navigate in any direction given any of the entities. Unfortunately, I have not found a combination of [Key], [ForeignKey], and navigation properties that allows this more than once per entity.

For example, this works (Entity Framework thinks TableA is the principal in the 1:1 relationship):

public class TableA
{
    [Key]
    [Required]
    public Guid Id { get; set; }

    public virtual TableB TableB { get; set; }
    public virtual TableC TableB { get; set; }
}

public class TableB
{
    [Key]
    [ForeignKey("TableA")]
    [Required]
    public Guid Id { get; set; }

    public virtual TableA TableA { get; set; }
}

but this doesn't work:

public class TableB
{
    [Key]
    [ForeignKey("TableA,TableC")] // The constructor documentation says this will work, but at runtime it throws an exception
    [Required]
    public Guid Id { get; set; }

    public virtual TableA TableA { get; set; }

    public virtual TableC TableC { get; set; }
}

nor does this:

public class TableB
{
    [Key]
    [Required]
    public Guid Id { get; set; }

    [ForeignKey("Id")]
    public virtual TableA TableA { get; set; }

    [ForeignKey("Id")]
    public virtual TableC TableC { get; set; }
}

How can I use attributes to allow these relationships? Currently, if I want to navigate to TableC from TableB, I must navigate through TableA, causing an extra unnecessary database lookup. It seems Entity Framework wants to force a hierarchical relationship where there may be none.

octavioccl

I think what you need is a model like this:

public class TableA
{
    [Key]
    public Guid Id { get; set; }

    public virtual TableB TableB { get; set; }
    public virtual TableC TableC { get; set; }
}
public class TableB
{
    [Key]
    [ForeignKey("TableA")]
    public Guid Id { get; set; }

    public virtual TableA TableA { get; set; }

    public virtual TableC TableC { get; set; }
}

public class TableC
{
    [Key]
    [ForeignKey("TableB")]
    public Guid Id { get; set; }

    public virtual TableB TableB { get; set; }

    [Required]
    public virtual TableA TableA { get; set; }
}

You don't need to specify a FK property for TableC navigation property in the TableB entity because TableB is principal in the relationship between B and C.

Update

Try removing the Required data annotation and configuring the relationship this way, overriding the OnModelCreating method on your context:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
        modelBuilder.Entity<TableA>().HasOptional(a => a.TableC).WithOptionalPrincipal(c => c.TableA);

        base.OnModelCreating(modelBuilder);
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Code First Entity Framework 6: 1 to 1 with composite key

From Dev

Entity Framework 6 multiple table to one foreign key relationship code first

From Dev

Entity Framework 6 Code First Foreign Key Without Corresponding Properties

From Dev

Entity framework code first cant create primary and foreign key relationship

From Dev

Defining multiple Foreign Key for the Same table in Entity Framework Code First

From Dev

Multiple Foreign Key for Same table in Entity Framework Code First

From Dev

Can I create a bidirectional foreign key relationship using Entity Framework Code First data annotations?

From Dev

Entity Framework - Multiple 1 to 0..1 relationships using the same Key

From Dev

In Entity Framework code first, to map a navigation property do I also need to map the foreign key column

From Dev

How to specify a foreign key with code-first Entity Framework

From Dev

Entity Framework, Code First: How can i make a Foreign Key Not-Nullable

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Foreign Key in Code First Entity Framework

From Dev

Entity Framework: Foreign Key in code first

From Dev

Entity Framework 6 could not create a foreign key

From Dev

Relationships in Entity Framework Code First

From Dev

Entity Framework Code First - Relationships

From Dev

Entity Framework 1 to 1 relationship using code first. how?

From Dev

Entity framework 6 code first saving collection via foreign key relation

From Dev

Entity Framework Code first Foreign Key may cause cycles or multiple cascade paths

From Dev

Entity Framework Code First Relationships: One to Many to Multiple Entities

From Dev

Entity Framework Multiple Many to Many Relationships using Code First

From Dev

How do I create a new column and move data to it with Entity Framework Code First

From Dev

Entity Framework Code First Foreign Key adding Index as well

From Dev

Entity Framework Code First and Firebird - Foreign Key name issue

From Dev

Entity Framework Code first adds unwanted foreign key column

From Dev

Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

From Dev

Get foreign key value using Entity Framework code first

Related Related

  1. 1

    Code First Entity Framework 6: 1 to 1 with composite key

  2. 2

    Entity Framework 6 multiple table to one foreign key relationship code first

  3. 3

    Entity Framework 6 Code First Foreign Key Without Corresponding Properties

  4. 4

    Entity framework code first cant create primary and foreign key relationship

  5. 5

    Defining multiple Foreign Key for the Same table in Entity Framework Code First

  6. 6

    Multiple Foreign Key for Same table in Entity Framework Code First

  7. 7

    Can I create a bidirectional foreign key relationship using Entity Framework Code First data annotations?

  8. 8

    Entity Framework - Multiple 1 to 0..1 relationships using the same Key

  9. 9

    In Entity Framework code first, to map a navigation property do I also need to map the foreign key column

  10. 10

    How to specify a foreign key with code-first Entity Framework

  11. 11

    Entity Framework, Code First: How can i make a Foreign Key Not-Nullable

  12. 12

    Entity Framework Code First Foreign Key issue

  13. 13

    Entity Framework Code First Foreign Key issue

  14. 14

    Foreign Key in Code First Entity Framework

  15. 15

    Entity Framework: Foreign Key in code first

  16. 16

    Entity Framework 6 could not create a foreign key

  17. 17

    Relationships in Entity Framework Code First

  18. 18

    Entity Framework Code First - Relationships

  19. 19

    Entity Framework 1 to 1 relationship using code first. how?

  20. 20

    Entity framework 6 code first saving collection via foreign key relation

  21. 21

    Entity Framework Code first Foreign Key may cause cycles or multiple cascade paths

  22. 22

    Entity Framework Code First Relationships: One to Many to Multiple Entities

  23. 23

    Entity Framework Multiple Many to Many Relationships using Code First

  24. 24

    How do I create a new column and move data to it with Entity Framework Code First

  25. 25

    Entity Framework Code First Foreign Key adding Index as well

  26. 26

    Entity Framework Code First and Firebird - Foreign Key name issue

  27. 27

    Entity Framework Code first adds unwanted foreign key column

  28. 28

    Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

  29. 29

    Get foreign key value using Entity Framework code first

HotTag

Archive