2017-09-21 26 views
0

因此,我试图创建学生&主管对象之间的多对多关系,但我想明确定义我的StudentSupervisor类,因为我需要在此交汇点类中具有其他属性。在实体框架中为多对多关系禁用隐式创建的联结表

问题是,当我检查迁移文件时,EF会自动/隐式地创建多对多关系的联结表,我不想这样做。

隐式创建的表EF与我自定义的结点类StudentSupervisor具有相同的名称,除了结尾为1。 (StudentSupervisor1)。

我不希望这个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我用流利的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表中创建的迁移文件中看到,有另一个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); 
+0

您需要发布实体类的代码。没有看到你目前在做什么,我们无法帮助你。 –

+0

我已经编辑了代码。谢谢。请帮忙。 – StudyProgramming

回答

1

你得到其他表,因为

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

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

你告诉EF这Student有着直接关系到SupervisorSupervisor:下列属性的与Student有直接关系。为了支持EF必须生成一个与这两个实体相关的表格。

StudentSupervisorStudentsSupervisor集合有SupervisorStudents的集合。您也可以摆脱流畅的配置,只使用注释。以下应该会给你正确的结果。我自己还没有机会尝试过。如果我今天早上有时间,我会给它一个镜头,如果需要更新。

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; } 
} 
+0

好的。所以我想我也需要手动定义所有的关系,而不是依赖于EF来理解和创建结点类。我知道了!这是我正在寻找的。但是,在这种情况下,这是一个很好的做法吗? – StudyProgramming

+0

如果您需要结点表上的其他数据,那么这是一个很好的做法(即唯一的方法)。 –