2017-07-31 74 views
0

我試圖創建一個帶有2個表「學生」和「類」的EF代碼優先模型。通常EF會自動創建一個映射表,但我更喜歡擁有自己的映射表,因此它是可查詢的。當我在MSQL中創建我的模型時,它看起來應該如何,但出於某種原因,在我的遷移中,我看到EF試圖兩次創建表。EF多對多自定義中介表但遷移創建2

我的學生類

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 

namespace test.Models 
{ 
    public class Student 
    { 

     public Student() 
     { 
      Classes = new HashSet<Class>(); 
     } 


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

     [Required] 
     [StringLength(100)] 
     public string Name { get; set; } 

     [Required] 
     public DateTime DateOfBirth { get; set; } 

     [Required] 
     public Gender Gender { get; set; } 

     [Required] 
     public int TotalHours { get; set; } 

     [Required] 
     public int HoursStudied { get; set; } 

     public DateTime DateJoined { get; set; } 

     public ICollection<Class> Classes { get; set; } 
     public string FirstName { get; set; } 


     public int HoursLeft 
     { 
      get 
      { 
       return this.TotalHours - this.HoursStudied; 
      } 
     } 
    } 
} 

Class類

using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace test.Models 
{ 
    public class Class 
    { 

     public Class() 
     { 
      Students = new HashSet<Student>(); 
     } 

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

     [Required] 
     [StringLength(50)] 
     public string ClassName { get; set; } 

     [Required] 
     [Range(1, 100)] 
     public int MaxStudents { get; set; } 

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

    } 
} 

ClassStudents類

using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 

namespace test.Models 
{ 
    public class ClassStudents 
    { 
     public Class Class { get; set; } 

     public Student Student { get; set; } 

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

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

SchoolContext類

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Linq; 
using System.Web; 

namespace test.Models 
{ 
    public class SchoolContext : DbContext 
    { 
     public DbSet<Class> Classes { get; set; } 
     public DbSet<Student> Students { get; set; } 
     public DbSet<ClassStudents> ClassStudents { get; set; } 

     public SchoolContext() : base("SchoolManagementPortalEntities") 
     { 

     } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Entity<Class>() 
       .HasMany(c => c.Students) 
       .WithMany(c => c.Classes); 
     } 
    } 
} 

現在這裏是它創建的怪異遷移,請注意ClassStudents1表

namespace test.Migrations 
{ 
    using System; 
    using System.Data.Entity.Migrations; 

    public partial class InitialModel : DbMigration 
    { 
     public override void Up() 
     { 
      CreateTable(
       "dbo.Classes", 
       c => new 
        { 
         Id = c.Int(nullable: false, identity: true), 
         ClassName = c.String(nullable: false, maxLength: 50), 
         MaxStudents = c.Int(nullable: false), 
        }) 
       .PrimaryKey(t => t.Id); 

      CreateTable(
       "dbo.Students", 
       c => new 
        { 
         Id = c.Int(nullable: false, identity: true), 
         Name = c.String(nullable: false, maxLength: 100), 
         DateOfBirth = c.DateTime(nullable: false), 
         Gender = c.Int(nullable: false), 
         TotalHours = c.Int(nullable: false), 
         HoursStudied = c.Int(nullable: false), 
         DateJoined = c.DateTime(nullable: false), 
         FirstName = c.String(), 
        }) 
       .PrimaryKey(t => t.Id); 

      CreateTable(
       "dbo.ClassStudents", 
       c => new 
        { 
         ClassId = c.Int(nullable: false), 
         StudentId = c.Int(nullable: false), 
        }) 
       .PrimaryKey(t => new { t.ClassId, t.StudentId }) 
       .ForeignKey("dbo.Classes", t => t.ClassId, cascadeDelete: true) 
       .ForeignKey("dbo.Students", t => t.StudentId, cascadeDelete: true) 
       .Index(t => t.ClassId) 
       .Index(t => t.StudentId); 

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

     } 

     public override void Down() 
     { 
      DropForeignKey("dbo.ClassStudents", "StudentId", "dbo.Students"); 
      DropForeignKey("dbo.ClassStudents", "ClassId", "dbo.Classes"); 
      DropForeignKey("dbo.ClassStudents1", "Student_Id", "dbo.Students"); 
      DropForeignKey("dbo.ClassStudents1", "Class_Id", "dbo.Classes"); 
      DropIndex("dbo.ClassStudents1", new[] { "Student_Id" }); 
      DropIndex("dbo.ClassStudents1", new[] { "Class_Id" }); 
      DropIndex("dbo.ClassStudents", new[] { "StudentId" }); 
      DropIndex("dbo.ClassStudents", new[] { "ClassId" }); 
      DropTable("dbo.ClassStudents1"); 
      DropTable("dbo.ClassStudents"); 
      DropTable("dbo.Students"); 
      DropTable("dbo.Classes"); 
     } 
    } 
} 

現在我不知道爲什麼會發生,但由於某種原因,有2個classStudents表內,以亂用我的代碼。

任何幫助,將不勝感激。乾杯!

+0

我喜歡[此方法](https://stackoverflow.com/questions/7050404/create-code-first-many-to-many-with-additional-fields-in-association-table)對於多對多。 –

回答

0

我的答案是基於關閉此教程: http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

這是你可能會錯過了什麼:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<Class>() 
       .HasMany<Student>(s => s.Students) 
       .WithMany(c => c.Classes) 
       .Map(cs => 
        { 
         cs.MapLeftKey("ClassId"); 
         cs.MapRightKey("StudentId"); 
         cs.ToTable("ClassStudents"); 
        }); 
} 
+0

感謝您的建議,我添加了它,但似乎沒有工作。我找到了從DbSet <>中移除我的中間表的答案。 –

0

如果我在的DbContext刪除DbSet它似乎是工作的罰款。