I have 2 entities with a many to many relationship:
[Table("Student", Schema = "School")]
public class Student
{
public long ID { get; set; }
public virtual List<Teacher> Teachers { get; set; }
//...
}
[Table("Teacher", Schema = "School")]
public class Teacher
{
public long ID { get; set; }
public virtual List<Student> Students { get; set; }
//...
}
I've specified in the fluent API how to construct the join table as such:
public StudentMap(string schema)
{
//Where schema = "School"
HasMany(p => p.Teachers)
.WithMany(p => p.Students)
.Map(m =>
{
m.ToTable("StudentsTeachers", schema);
m.MapLeftKey("Student_ID");
m.MapRightKey("Teacher_ID");
});
}
However, when I go to access the Teachers navigation object on the Student, it defaults to the EF convention as opposed to what I've designated for the join table. How would I go about specifying to the SchoolContext
that we should be looking at School.StudentsTeachers
table instead of dbo.StudentTeachers
?
The problem isn't in the designation of the join table, or the many to many relationship being generated. Those worked fine. The problem arises when attempting to use the Entities, I need a way to specify the relationship should use the Join table I specified, as opposed to the EF naming convention. I was able to resolve a similar issue with EF using its conventions over my table names by using the Table Attribute as shown above. I'm now looking for an equivalent answer except with regards to the many to many join table that exists, but doesn't have an explicit model