为 Entity Framework 中的多对多关系禁用隐式创建的联结 table

Disabling the Implicitly created junction table for many-to-many relationship in Entity Framework

因此,我正在尝试在 Student 和 Supervisor 对象之间创建多对多关系,但我想明确定义我的 StudentSupervisor class,因为我需要在此连接点中具有其他属性 class.

问题是,当我签入迁移文件时,EF automatically/implicitly 为多对多关系创建了联结 table,我不希望这样。

隐式创建的 table EF 与我自定义的结点 class StudentSupervisor 同名,只是最后加了 1。 (学生监督员 1)。

我不想自动创建这个 StudentSupervisor1。删除它不会做任何事情,因为在每次添加迁移时,它都会被重新创建。

提前致谢。

编辑:

public class Supervisor
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int FacultyId { get; set; }

    public Faculty Faculty { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

public class Student
{
    public int Id { get; set; }

    public string MatricNo { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Supervisor> Supervisors { get; set; }
}

public class SupervisorStudents
{
    [Key, Column(Order = 0)]
    public int SupervisorId { get; set; }

    public Supervisor Supervisor { get; set; }

    [Key, Column(Order = 1)]
    public int StudentId { get; set; }

    public Student Student { get; set; }

    public string TagMainOrCo { get; set; }
}

并且在我的 DbContext 中,我使用 Fluent API 来覆盖它们。

 modelBuilder
            .Entity<Student>()
            .HasKey(s => s.Id);

        modelBuilder
            .Entity<Supervisor>()
            .HasKey(p => p.Id);

        modelBuilder
            .Entity<SupervisorStudents>()
            .HasKey(a =>
                new
                {
                    a.StudentId, a.SupervisorId
                }
            );

        base.OnModelCreating(modelBuilder);

但是正如您在迁移文件中看到的那样,我创建了我的 SupervisorStudents table,还有另一个 SupervisorStudents1(由 EF 隐式创建)

CreateTable(
            "dbo.SupervisorStudents",
            c => new
                {
                    SupervisorId = c.Int(nullable: false),
                    StudentId = c.Int(nullable: false),
                    TagMainOrCo = c.String(),
                })
            .PrimaryKey(t => new { t.SupervisorId, t.StudentId })
            .ForeignKey("dbo.Students", t => t.StudentId, cascadeDelete: true)
            .ForeignKey("dbo.Supervisors", t => t.SupervisorId, cascadeDelete: true)
            .Index(t => t.SupervisorId)
            .Index(t => t.StudentId);

 CreateTable(
            "dbo.SupervisorStudents1",
            c => new
                {
                    Supervisor_Id = c.Int(nullable: false),
                    Student_Id = c.Int(nullable: false),
                })
            .PrimaryKey(t => new { t.Supervisor_Id, t.Student_Id })
            .ForeignKey("dbo.Supervisors", t => t.Supervisor_Id, cascadeDelete: true)
            .ForeignKey("dbo.Students", t => t.Student_Id, cascadeDelete: true)
            .Index(t => t.Supervisor_Id)
            .Index(t => t.Student_Id);

你得到另一个 table 因为以下属性:

public virtual ICollection<Student> Students { get; set; }

public virtual ICollection<Supervisor> Supervisors { get; set; }

您告诉 EF StudentSupervisor 有直接关系,SupervisorStudent 有直接关系。为了支持 EF 必须生成一个 table 来关联这两个实体。

StudentSupervisorStudents 的集合,SupervisorSupervisorStudents 的集合。您也可以摆脱流畅的配置,只使用注释。以下应该为您提供正确的结果。我还没有机会亲自尝试。如果我今天早上有时间,我会试一试并根据需要进行更新。

public class Supervisor
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public int FacultyId { get; set; }

    public virtual ICollection<SupervisorStudents> Students { get; set; }
}

public class Student
{
    [Key]
    public int Id { get; set; }

    public string MatricNo { get; set; }

    public string Name { get; set; }

    public virtual ICollection<SupervisorStudents> Supervisors { get; set; }
}

public class SupervisorStudents
{
    [Key, Column(Order = 0)]
    public int SupervisorId { get; set; }

    [ForeignKey(nameof(SupervisorId))]
    public Supervisor Supervisor { get; set; }

    [Key, Column(Order = 1)]
    public int StudentId { get; set; }

    [ForeignKey(nameof(StudentId))]
    public Student Student { get; set; }

    public string TagMainOrCo { get; set; }
}