public Summary Save(Summary summary, List<Activity> activities, List<Person> people = null) { if (summary.Published) return summary; //using (TransactionScope ts = new TransactionScope()) { if (summary.Id == 0) { _context.Summaries.Add(summary); } else { summary = _context.Summaries.Single(x => x.Id == summary.Id); var oldActivities = _context.Summaries .Include("SummaryActivities") .Include("SummaryActivities.Activity") .Single(x => x.Id == summary.Id) .SummaryActivities .Select(x => x.Activity) .ToList(); var activitiesToRemove = oldActivities.Except(activities, new ActivityEqualityComparer()).ToList(); var activitiesToAdd = activities.Except(oldActivities, new ActivityEqualityComparer()).ToList(); activities = activitiesToAdd.ToList(); foreach (var activityToRemove in activitiesToRemove) { var summaryActivitiesToRemove = _context.SummaryActivities.Single(x => x.ActivityId == activityToRemove.Id && x.SummaryId == summary.Id); _context.SummaryActivities.Remove(summaryActivitiesToRemove); } } foreach (var activity in activities) { SummaryActivity sa = new SummaryActivity { ActivityId = activity.Id, Summary = summary }; _context.SummaryActivities.Add(sa); if (people != null) { foreach (Person person in people.Where(p => p.ActivityId == activity.Id)) { Person newPerson = new Person { ActivityId = activity.Id, Summary = summary, BirthDate = person.BirthDate, EducationLevelId = person.EducationLevelId, FirstName = person.FirstName, LastName = person.LastName, Salary = person.Salary, Job = person.Job, JobLevelId = person.JobLevelId }; _context.Peoples.Add(newPerson); } } } _context.SaveChanges(); // ts.Complete(); return summary; } }
public ActionResult Edit(PersonViewModel person) { if (ModelState.IsValid) { Person p = new Person { Id = Int32.Parse(person.Id ?? "0"), FirstName = person.FirstName, LastName = person.LastName, BirthDate = DateTime.Now.AddYears(-30), Job = person.Job, ActivityId = Int32.Parse(person.ActivityId), SummaryId = Int32.Parse(person.SummaryId), EducationLevelId = Int32.Parse(person.EducationLevel), Salary = Decimal.Parse(person.Salary), JobLevelId = Int32.Parse(person.JobLevel) }; _personsRepository.Set(p); return this.RedirectToAction( "List", new { summaryId = p.SummaryId.ToString(), activityId = p.ActivityId.ToString() }); } return this.View(person); }