Reputation: 927
I am trying to seed my EF code first database in the Configuration file of the migrations folder, but somehow I have no idea how as I do not want to add a new entry to the related database.
I have the following database structure.
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; }
}
And have got the following Code in my migration configuration file:
// 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 });
Now what i want to do is something like this:
// 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 });
Another thing I tried was:
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 } });
However that does't work, also note that I cannot use new Classes() { Id = 1 }
as this will create a new entity.
Any help will be appreciated, Cheers!
Upvotes: 1
Views: 78
Reputation: 927
I couldn't get it to work with the method @gertArnold suggested, but after looking around for some time I found that it is possible to do it via the AddOrUpdate method and it will also check whether the value already exists or not. In that case it will be written as follows:
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
}
});
Hope it will help anyone.
Upvotes: 0
Reputation: 109109
You can add classrooms and classes to the collections in 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);
etc.
That's the gist of it. Add classes and classrooms that belong to a schedule to the ClassSchedule
s collections and add the schedule to the context. Add classes and classrooms that don't belong to a schedule (yet) to the DbSet
s of the context.
Upvotes: 1