I am trying to update an entity and all of it's relationships and I'm not sure if that's possible. If it isn't possible, what's the best way of going about it?
My ASP MVC Controller...
public ActionResult TimeSheetEdit(Timesheet timesheet, string submitType)
{
_timesheetRepository.Update(timesheet);
_timesheetRepository.Commit();
return RedirectToAction("TimeSheetEdit", new {id=timesheet.TimeSheetId});
}
My timesheet Model has multiple properties, one property is another entity (which has a list of entities). I was hoping EF would go map through all of my relationships and update everything, but it appears that it only updates the Timesheet model and ignores all the other associated entities.
Is this normal or am I missing something? If it's normal, what's the best practice for accomplishing what I'm trying to do?
Repository Update code...
public void Update(T entity)
{
_dbSet.Attach(entity);
_context.Entry(entity).State = EntityState.Modified;
}
public int Commit()
{
return _context.SaveChanges();
}
I'm not sure if it will help, but here are my Models
Timesheet
public class Timesheet
{
[Key]
public int TimeSheetId { get; set; }
public Guid Id { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public TimeSheetStatus TimeSheetStatus { get; set; }
public int EmployeeId { get; set; }
public virtual Employee Employee { get; set; }
public virtual List<TimesheetLine> TimesheetLines { get; set; }
}
TimesheetLine
public class TimesheetLine
{
[Key]
public int TimeSheetLineId { get; set; }
public Guid CustomerId { get; set; }
public string CustomerName { get; set; }
public Guid EarningsId { get; set; }
public virtual List<TimeSheetLineUnit> Units { get; set; }
public int TimeSheetId { get; set; }
public virtual Timesheet Timesheet { get; set; }
}
TimeSheetLineUnit
public class TimeSheetLineUnit
{
[Key]
public int UnitId { get; set; }
public decimal Hours { get; set; }
[DisplayName("Notes")]
public string Description { get; set; }
public int Index { get; set; }
public int TimeSheetLineId { get; set; }
public virtual TimesheetLine TimesheetLine { get; set; }
}