2017-07-31 76 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它似乎是工作的罚款。