EF多对多自定义中间表,但迁移创建2

时间:2017-07-31 06:50:59

标签: entity-framework ef-code-first

我试图创建一个包含2个表的#EF代码优先模型"学生"和"课程"。通常EF自动创建一个映射表,但我更喜欢拥有自己的映射表,因此它是可查询的。当我在MSQL中创建我的模型时,它看起来应该如何,但出于某些原因,在我的迁移中,我看到EF正在尝试创建表两次。

我的学生班

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

班级

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 Class

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");
        }
    }
}

现在我不知道为什么会这样,但由于某种原因,有两个classStudents表会搞乱我的代码。

任何帮助将不胜感激。干杯!

2 个答案:

答案 0 :(得分: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");
                    });
}

答案 1 :(得分:0)

如果我从DbContext中删除DbSet,它似乎工作正常。