public async Task SaveTutorAsync(Tutor tutor, string[] TypeTutors) { Tutor entry = context.Tutors .Include(x => x.TypeTutors) .Include(x => x.User) .Where(x => x.Id == tutor.Id) .FirstOrDefault(); entry.Id = tutor.Id; if (tutor.TrainingCourseDisciplineId != entry.TrainingCourseDisciplineId || tutor.TrainingCourseTeacherId != entry.TrainingCourseTeacherId) { await CreateTrainingCourseAsync(tutor.TrainingCourseDisciplineId, tutor.TrainingCourseTeacherId); entry.TrainingCourseDisciplineId = tutor.TrainingCourseDisciplineId; entry.TrainingCourseTeacherId = tutor.TrainingCourseTeacherId; } if (entry.TypeTutors == null) entry.TypeTutors = new List<TypeTutor>(); else entry.TypeTutors.Clear(); if (TypeTutors != null) { foreach (var item in TypeTutors) { TypeTutor type = await context.TypeTutor.Where(x => x.Name == item).FirstOrDefaultAsync(); entry.TypeTutors.Add(type); } } entry.Comment = tutor.Comment; context.Entry(entry).State = EntityState.Modified; await context.SaveChangesAsync(); }
public async Task CreateTutorAsync(Tutor tutor, string userName, string[] TypeTutors) { await CreateTrainingCourseAsync(tutor.TrainingCourseDisciplineId, tutor.TrainingCourseTeacherId); tutor.User = await context.Users .Where(x => x.UserName == userName) .FirstOrDefaultAsync(); if (tutor.TypeTutors==null) tutor.TypeTutors = new List<TypeTutor>(); foreach (var item in TypeTutors) { TypeTutor type = await context.TypeTutor.Where(x => x.Name == item).FirstOrDefaultAsync(); tutor.TypeTutors.Add(type); } context.Entry(tutor).State = EntityState.Added; await context.SaveChangesAsync(); }