public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection
public class Course
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection
public class ApplicationDbContext : DbContext
{
public DbSet
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity()
        .HasMany(s => s.Courses)
        .WithMany(c => c.Students)
        .Map(cs =>
        {
            cs.MapLeftKey("StudentId");
            cs.MapRightKey("CourseId");
            cs.ToTable("StudentCourses");
        });
}
 
}
public class CourseRepository : IDisposable { private ApplicationDbContext context = new ApplicationDbContext();
public void AddCoursesToStudent(Student student, List courses)
{
    student.Courses = courses;
    context.SaveChanges();
}
public List GetCoursesForStudent(int studentId)
{
    return context.Students
        .Where(s => s.Id == studentId)
        .SelectMany(s => s.Courses)
        .ToList();
}
public void Dispose()
{
    context.Dispose();
}
  
}
public class HomeController : Controller { private CourseRepository courseRepository = new CourseRepository();
public ActionResult Index()
{
    ViewBag.Courses = courseRepository.GetCoursesForStudent(1);
    return View();
}
[HttpPost]
public ActionResult SaveCourses()
{
    int studentId = int.Parse(Request.Form["studentId"]);
    List courses = Request.Form["courses"]
        .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
        .Select(courseId => new Course { Id = int.Parse(courseId) })
        .ToList();
    courseRepository.AddCoursesToStudent(studentId, courses);
    return RedirectToAction("Index");
}
 
}
@model ProjectName.Models.Student
@{ ViewBag.Title = "Index";