private readonly ApplicationDbContext _context; public HomeController(ApplicationDbContext context) { _context = context; } public IActionResult Index() { //add grades var grades = new List() { new Grade(){GradeName = "Grade 1" }, new Grade(){GradeName = "Grade 2" }, }; _context.Grades.AddRange(grades); _context.SaveChanges(); //find the existing grade var grade1 = _context.Grades.FirstOrDefault(c => c.GradeName == "Grade 1"); //if the grade is not exist, create a new grade //add student with grade. var student = new List() { new Student() { Name = "Tom", Grade = grade1 }, new Student() { Name = "John", Grade = grade1 }, }; _context.Students.AddRange(student); _context.SaveChanges(); //query grades and the relates students var queryresult = _context.Grades.Include(c => c.Students).ToList(); return View(); }