EF 6.1.3如何播种多对多关系

时间:2017-07-06 23:08:21

标签: entity-framework ef-code-first

我正在尝试在迁移文件夹的配置文件中播放我的EF代码第一个数据库,但不知怎的,我不知道我怎么不想在相关数据库中添加新条目。

我有以下数据库结构。

public class ClassSchedule
    {
        public int Id { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }

        public virtual Class Classes { get; set; }
        public virtual Classroom Classrooms { get; set; }
    }

public class Class
    {
        public Class()
        {
            this.Students = new HashSet<Student>();
            this.ClassSchedules = new HashSet<ClassSchedule>();
        }

        public int Id { get; set; }
        public int LevelId { get; set; }

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

        public virtual Level Levels { get; set; }
        public ICollection<Student> Students { get; set; }
        public ICollection<ClassSchedule> ClassSchedules { get; set; }
    }

  public class Classroom
    {
        public Classroom()
        {
            this.ClassSchedules = new HashSet<ClassSchedule>();
        }

        public int Id { get; set; }
        public string ClassRoomName { get; set; }
        public int MaxStudents { get; set; }

        public ICollection<ClassSchedule> ClassSchedules { get; set; }
    }

我的迁移配置文件中包含以下代码:

// all classrooms
  context.Classrooms.Add(new Classroom() { ClassRoomName = "Cherry", MaxStudents = 20 });
  context.Classrooms.Add(new Classroom() { ClassRoomName = "Pear", MaxStudents = 12 });
  context.Classrooms.Add(new Classroom() { ClassRoomName = "Apple", MaxStudents = 12 });


  // classes
  context.Classes.Add(new Class() { ClassName = "YR1", MaxStudents = 12, LevelId = 1 });
  context.Classes.Add(new Class() { ClassName = "YB1", MaxStudents = 6, LevelId = 3 });
  context.Classes.Add(new Class() { ClassName = "IELTS L1", MaxStudents = 40, LevelId = 14 });

现在我想做的是这样的事情:

// ClassRoom schedules, links the class and classroom on a certain time
            context.ClassSchedules.Add(new ClassSchedule() { StartTime = DateTime.Parse("2017-09-17T18:00:00"), EndTime = DateTime.Parse("2017-09-17T20:00:00"), Classes = 1, Classrooms = 1 });

我尝试的另一件事是:

   context.ClassSchedules.Add(new ClassSchedule() { StartTime = DateTime.Parse("2017-09-17T18:00:00"), EndTime = DateTime.Parse("2017-09-17T20:00:00"), Classes = { Id = 1 }, Classrooms = { Id = 2 } });

然而,这不起作用,同时请注意我不能使用new Classes() { Id = 1 },因为这将创建一个新实体。

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

2 个答案:

答案 0 :(得分:1)

您可以在ClassSchedule

中为教室添加课堂和课程
var classRoom1 = new Classroom { ClassRoomName = "Cherry", MaxStudents = 20 };
var class1 = new Class { ClassName = "YR1", MaxStudents = 12, LevelId = 1 };
var classSchedule1 = new ClassSchedule { StartTime = ... };

classSchedule1.Classrooms.Add(classRoom1);
classSchedule1.Classess.Add(class1);

context.ClassSchedules.Add(classSchedule1);

这就是它的要点。将属于计划的类和教室添加到ClassSchedule集合中,并将计划添加到上下文中。将不属于时间表的课程和课堂添加到上下文的DbSet

答案 1 :(得分:0)

我无法使用@gertArnold建议的方法,但在查看了一段时间之后我发现可以通过AddOrUpdate方法完成它并且它还将检查值是否已经存在。在这种情况下,它将写成如下:

 context.ClassSchedules.AddOrUpdate(cs => cs.ScheduleName,
        new ClassSchedule
        {
            ScheduleName = "Yippie red class",
            StartTime = DateTime.Parse("2017-09-17T18:00:00"),
            EndTime = DateTime.Parse("2017-09-17T20:00:00"),
            Classes = new Class()
            {
                ClassName = "YR1",
                MaxStudents = 12,
                LevelId = 1
            },
            Classrooms = new Classroom()
            {
                ClassRoomName = "Cherry",
                MaxStudents = 20
            }

        });

希望它能帮助任何人。