0

I have this Scenario:

public class Table1
{
    [Key]
    public string Table1Code { get; set; }

    public virtual List<Table2> Table2 { get; set; }
}

public class Table2
{
    [Key]
    public string Table2Code { get; set; }

    public virtual List<Table1> Table1 { get; set; }    
}

Then I create a configuration class for specifying the many to many table:

public class Table1Configuration : EntityTypeConfiguration<Table1>
{
    public Table1Configuration()
    {
        HasMany(g => g.Table2)
            .WithMany(r => r.Table1)
            .Map(c =>
            {
                c.ToTable("Table1_Table2");
                c.MapLeftKey("Table1Code");
                c.MapRightKey("Table2Code");
            });
    }
}

Now I have to create a Table3 like this

public class Table3
{
    [Key]
    public string Table3Code { get; set; }

    public string Table1Code { get; set; }
    public string Table2Code { get; set; }
}

How can I add the foreign key for columns Table1Code and Table2Code to the table Table1_Table2?

I don't need to add the foreign key to Table1 and Table2 but to the table Table1_Table2.

poke
  • 369,085
  • 72
  • 557
  • 602
Marco
  • 71
  • 2
  • 8

2 Answers2

1

Not sure you can do that without an explicit Table1_Table2 class:

public class Table1_Table2
{
    public string Table1Code { get; set; }  // PK 1
    public string Table2Code { get; set; }  // PK 2
    public virtual Table3 Table3 { get; set; }    
}

Then:

public class Table3
{
    // Not needed in 1:1
    // [Key]
    // public string Table3Code { get; set; }
    public string Table1Code { get; set; }
    public string Table2Code { get; set; }
    // Make this a collection for 1:M
    public virtual Table1_Table2 Table1_Table2 { get; set; }    
}

Fluent code:

modelBuilder.Entity<Table3>()
    .HasKey(t3 => new { t3.Table1Code, t3.Table2Code });

modelBuilder.Entity<Table1_Table2>()
    .HasOptional(t => t.Table3)
    .WithRequired(t3 => t3.Table1_Table2);
Steve Greene
  • 12,029
  • 1
  • 33
  • 54
0

A Many2Many(M2M) Relation like you have made already using EF creates a table which has foregin keys to tables with entities in a M2M relation.

So going by what you have done in classes Table1 and Table2 a third mapping table will be created by EF itself. So there is no special need to create a third table Table3

But if for domain reason you want to create a third mapping entity Table2 which maps Table1 and Table2 you will have to modify the classes in the following way.

public class Table1
{
    [Key]
    public string Table1Code { get; set; }

    public virtual List<Table3> Table3 { get; set; }
}

public class Table2
{
    [Key]
    public string Table2Code { get; set; }

    public virtual List<Table3> Table3 { get; set; }    
}

 public class Table3
{
    [Key]
    public string Table3Code { get; set; }

    public Table1 Table1 { get; set; }
    public string Table1Code { get; set; }

    public Table2 Table2 { get; set; }
    public string Table2Code { get; set; }
}
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
  • Thanks for the answer but I would like to have the Table3 populated only with the data in the table Table1_Table2. in your answer Table3 may have data that is not present in table Table1_Table2 – Marco Sep 20 '17 at 10:47