-1

I am using EF6 to access a set of three tables "TableA", "TableB" and "TableC" TableA has a many to many relationship with TableB and TableC so there are two tables that were created TableATableB & TableATableC. These tables had two columns in each,i.e. TableA_Id and TableB_Id or TableA_Id and TableC_Id. This works but now the DBA wants me to remove the underscore character from those columns.

While the original tables was created with migrations I have since removed the migrations aspect from the project. I have renamed the columns in the actual join tables but how do I get the relationship back to TableA to work? Is it possible via data annotations or fluent?

TableA:

 public class TableA
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public virtual ICollection<TableB> TableBs { get; set; }
    public virtual ICollection<TableC> TableCs { get; set; }
}

TableB:

 public class TableB
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<TableA> TableAs { get; set; }
}

TableC:

 public class TableC
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<TableA> TableAs { get; set; }
}

This is different then previous questions because it specifies that the join table is already created and has specific column names. The problem is how to get existing code to map to the correct column

names.

John S
  • 7,909
  • 21
  • 77
  • 145

1 Answers1

2

I would go with the fluent and you can mention the keys as below. for better understanding, bellow is an example of Student and Courses

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    modelBuilder.Entity<Student>()
                .HasMany<Course>(s => s.Courses)
                .WithMany(c => c.Students)
                .Map(cs =>
                        {
                            cs.MapLeftKey("StudentId");
                            cs.MapRightKey("CourseId");
                            cs.ToTable("StudentCourse"); // this is mappingtable
                        });

}

The above example is best explained here in the Blog you can say the blog clearly explains many to many relationship

RJ-
  • 136
  • 1
  • 13
  • That still gives me an invalid column name (with the old column name containing the underscore) – John S Oct 19 '17 at 20:10
  • Stupid mistake... I was building a release version and testing a debug version so the debug version wasn't seeing any of the changes to my code. – John S Oct 19 '17 at 20:27