I'm trying to make many to many relationships between two classes with EF Core fluent API. How can I change the foreign key names of the table between that will be created for this relationship?
For example, if we create a many-to-many relationship between the following two classes:
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
public string Description { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
// Fluent Api
public class UserMap : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.HasKey(x => x.Id);
builder.ToTable("Users");
builder.HasMany<Course>(user => user.Courses)
.WithMany(course => course.Users);
}
}
A table is created like this:
What I want is to create the in-between table with the foreign key names I give during code-first.
I know I can write the in-between table as a class and change the names with a one-to-many relationship, but I want to do it with code-first.
Example taken from EF core documentation.