public void CopyFrom(ExpenseReport report)
 {
     this.Name = report.Name;
     this.Purpose = report.Purpose;
     this.Created = report.Created;
     this.Submitted = report.Submitted;
     this.Comments = report.Comments;
     this.ApproverName = report.ApproverName;
     this.StatusId = report.StatusId;
     this.UserId = report.UserId;            
 }
 public JsonResult GetOrUpdate(ExpenseReport report)
 {
     return this.CreateOrUpdate(report);
 }
        private JsonResult CreateOrUpdate(ExpenseReport report)
        {
            try
            {
                report.UserId = this.GetUserId();
                if (report.StatusId == ReportsContextInitializer.PendingStatus)
                {
                    report.Submitted = DateTime.Now;
                }

                this.expenseReportRepository.Save(report);
                return Json(new { success = true, reportId = report.Id });
            }
            catch (Exception)
            {
                return Json(new { error = "error" });
            }
        }
        private ExpenseReport LoadReferences(DbContext ctx, ExpenseReport report)
        {
            if (report != null)
            {
                ctx.Entry(report).Collection(r => r.Details).Load();
                ctx.Entry(report).Reference(r => r.User).Load();
                ctx.Entry(report).Reference(r => r.Status).Load();
            }

            return report;
        }
        public ExpenseReport Save(ExpenseReport report)
        {
            using (var ctx = this.GetContext())
            {
                var targetReport = this.LoadReferences(ctx, ctx.ExpenseReports.Find(report.Id));

                if (targetReport == null)
                {
                    if (report.Details != null)
                    {
                        report.Details.ToList().ForEach(d =>
                        {
                            d.Report = report;
                            ctx.ExpenseReportDetails.Add(d);
                        });
                    }
                    else
                    {
                        report.Details = new List<ExpenseReportDetail>();
                    }

                    ctx.ExpenseReports.Add(report);
                }
                else
                {
                    targetReport.CopyFrom(report);

                    if (report.Details != null)
                    {
                        targetReport.Details.Where(d => !report.Details.Contains(d))
                                    .ToList()
                                    .ForEach(d =>
                                    {
                                        ctx.ExpenseReportDetails.Remove(d);
                                    });

                        report.Details.Where(d => targetReport.Details.Contains(d))
                                        .ToList()
                                        .ForEach(d =>
                                        {
                                            var targetExpense = targetReport.Details.Where(e => e.Id.Equals(d.Id)).FirstOrDefault();
                                            targetExpense.CopyFrom(d);
                                        });

                        report.Details.Where(d => !targetReport.Details.Contains(d))
                                      .ToList()
                                      .ForEach(d =>
                                        {
                                            d.Report = targetReport;
                                            ctx.ExpenseReportDetails.Add(d);
                                        });
                    }
                    else
                    {
                        targetReport.Details.ToList().ForEach(d => ctx.ExpenseReportDetails.Remove(d));                        
                    }
                }

                ctx.SaveChanges();

                return report;
            }
        }