C# 什么';对于.Net核心(EF核心)中的多对多关系,什么是最佳解决方案?

C# 什么';对于.Net核心(EF核心)中的多对多关系,什么是最佳解决方案?,c#,.net-core,many-to-many,relationship,ef-core-3.0,C#,.net Core,Many To Many,Relationship,Ef Core 3.0,正如您所知,我们在EF Core中的实体之间没有自动的多对多关系。实现这一目标的最佳解决方案是什么? 以下是我为实现此目的而创建的内容: class Student { public int StudentId { get; set; } public string Name { get; set; } public string Family { get; set; } public List<StudentCourse> Courses { g

正如您所知,我们在EF Core中的实体之间没有自动的多对多关系。实现这一目标的最佳解决方案是什么? 以下是我为实现此目的而创建的内容:

 class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    public string Family { get; set; }
    public List<StudentCourse> Courses  { get; set; }

}
class Course
{
    public int CourseId { get; set; }
    public string Name { get; set; }
    public List<Student> Students { get; set; }
}
class StudentCourse
{
    public int StudentCourseId { get; set; }
    public Student Student { get; set; }
    public Course Course { get; set; }
}
班级学生
{
公共int StudentId{get;set;}
公共字符串名称{get;set;}
公共字符串族{get;set;}
公共列表课程{get;set;}
}
班级课程
{
public int CourseId{get;set;}
公共字符串名称{get;set;}
公共列表学生{get;set;}
}
班级学生课程
{
public int StudentCourseId{get;set;}
公立学生学生{get;set;}
公共课程{get;set;}
}

在您的示例中,您的操作是正确的,但我想说,
课程
实体也应该有一个
列表
,而不是
列表新的{sc.StudentId,sc.CourseId});
builder.HasOne(sc=>sc.Student)
.有许多(s=>s.学生课程)
.HasForeignKey(sc=>sc.StudentId);
builder.HasOne(sc=>sc.Course)
.有许多(c=>c.学生课程)
.HasForeignKey(sc=>sc.CourseId);
}
公共班级学生
{
公共int StudentId{get;set;}
公共列表学生课程{get;set;}
}
公共课
{
public int CourseId{get;set;}
公共列表学生课程{get;set;}
}
公共课学生课程
{
公共int StudentId{get;set;}
公立学生学生{get;set;}
public int CourseId{get;set;}
公共课程{get;set;}
}

只有一个随机副本。有更多的候选人。不确定问题是什么。如果你想实现“自动”多对多,显然你不能,因为EFC还不支持它。通过显式连接实体实现多对多所需的一切都在。我知道如何实现这一点,我说也许有比我更好的解决方案。比文档更好?