private async Task DeleteSteve(ManyToManyContext ctx) { Student steve = await ctx.Students.FirstAsync(s => s.StudentNum == 123456); ctx.Students.Remove(steve); await ctx.SaveChangesAsync(); }
private async Task UnEnrollSteveFromCourse(ManyToManyContext ctx) { StudentCourse steveAndDnp = ctx.Students. Where(s => s.StudentNum == 123456) .SelectMany(student => student.StudentCourses). First(studentCourse => studentCourse.Course.CourseCode.Equals("IT-SDJ2-A20")); ctx.Remove(steveAndDnp); await ctx.SaveChangesAsync(); }
private async Task AddOneStudent(ManyToManyContext ctx) { Student s = new Student { FirstName = "Steve", LastName = "Doe", Email = "*****@*****.**", StudentNum = 123456 }; await ctx.Students.AddAsync(s); await ctx.SaveChangesAsync(); }
private async Task WhichCoursesIsSteveEnrolledIn(ManyToManyContext ctx) { List <Course> courses = await ctx.Students .Where(s => s.StudentNum == 123456) .SelectMany(student => student.StudentCourses) .Select(studentCourse => studentCourse.Course) .ToListAsync(); Console.WriteLine(JsonSerializer.Serialize(courses, new JsonSerializerOptions { WriteIndented = true })); }
private async Task EnrollSteveInDNP(ManyToManyContext ctx, string courseCode) { Student steve = await ctx.Students.FirstAsync(s => s.StudentNum == 123456); Course dnp = await ctx.Courses.FirstAsync(c => c.CourseCode.Equals(courseCode)); StudentCourse sc = new StudentCourse { Course = dnp, Student = steve }; steve.StudentCourses = new List <StudentCourse>(); steve.StudentCourses.Add(sc); ctx.Update(steve); // ctx.Set<StudentCourse>().Add(sc); This is an alternative await ctx.SaveChangesAsync(); }
public async Task RunExample() { using (ManyToManyContext ctx = new ManyToManyContext()) { // await AddOneStudent(ctx); // await AddTwoCourses(ctx); // await EnrollSteveInDNP(ctx, "IT-DNP1Y-A20"); // await EnrollSteveInDNP(ctx, "IT-SDJ2-A20"); // await WhichCoursesIsSteveEnrolledIn(ctx); // await DeleteSteve(ctx); await UnEnrollSteveFromCourse(ctx); List <Course> selectSpecific = await SelectSpecific(ctx, course => new Course { Name = course.Name } ); } }
private async Task AddTwoCourses(ManyToManyContext ctx) { Course sdj2 = new Course { Abbreviation = "SDJ2", Name = "Software Development with UML and Java 2", Semester = 2, CourseCode = "IT-SDJ2-A20", ECTS = 10 }; Course dnp1 = new Course { Abbreviation = "DNP1", Name = "I forgot the actual name", Semester = 3, CourseCode = "IT-DNP1Y-A20", ECTS = 5 }; await ctx.Courses.AddAsync(sdj2); await ctx.Courses.AddAsync(dnp1); await ctx.SaveChangesAsync(); }
private async Task <List <Course> > SelectSpecific(ManyToManyContext ctx, Expression <Func <Course, Course> > expression) { List <Course> enumerable = ctx.Courses.Select(expression).ToList(); return(enumerable); }