In Entity Framework Core 1.0.1 Many-To-Many without creating relational entity is not supported and the easiest way to implement is creating the relational entity.
I will show that in a small example:
public class Course { public int Id { get; set; } public string Title { get; set; } public List<StudentAndCourse> StudentAndCourse { get; set; } }
public class Student { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public List<StudentAndCourse> StudentAndCourse { get; set; } }
Relational Entity:
public class StudentAndCourse { public int StudentId { get; set; } public Student Student{ get; set; } public int CourseId { get; set; } public Course Course { get; set; } }
The Fluent API in DbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<StudentAndCourse>() .HasKey(sc => new { sc.StudentId, sc.CourseId }); modelBuilder.Entity<StudentAndCourse>() .HasOne(sc => sc.Student) .WithMany(s => s.StudentAndCourse) .HasForeignKey(sc => sc.StudentId); modelBuilder.Entity<StudentAndCourse>() .HasOne(sc => sc.Course) .WithMany(c => c.StudentAndCourse) .HasForeignKey(sc => sc.CourseId); }