private void button1_Click_1(object sender, EventArgs e) { using (var ctx = new ScheduleContext()) { if (textBox2.Text == "" || textBox3.Text == "") { MessageBox.Show("Missing required input.", "Missing input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { var classroom = new Classroom { Name = textBox2.Text.ToString(), Capacity = Convert.ToInt32(textBox3.Text) }; ctx.Classrooms.Add(classroom); ctx.SaveChanges(); var len = ctx.Classrooms.ToArray().Length; this.Close(); //Adding ClassroomTimeRestriction foreach (var r in restrictions) { var restriction = new ClassroomTimeRestriction { TimeslotId = r, }; ctx.ClassroomTimeRestrictions.Add(restriction); ctx.SaveChanges(); } } } }