public CheckIn Create(CheckIn item) { if (item.IsValid()) { // to make sure that we don't re-insert the task if (item.TaskLog != null) { long taskLogId = (item.TaskLog != null) ? item.TaskLog.ID : item.TaskLogID.Value; item.TaskLog = db.TaskLogs.Where(t => t.ID == taskLogId).Single(); if (item.TaskCheckIns == null || item.TaskCheckIns.Count == 0) { item.TaskCheckIns = new List <TaskCheckIn> { new TaskCheckIn { TaskID = item.TaskLog.TaskID.Value } }; } } try { db.CheckIns.Add(item); db.SaveChanges(); MarkNotIsNew(item); return(item); } catch (Exception ex) { db.CheckIns.Remove(item); throw ex; } } else { throw new Exception("CheckIn is invalid"); } }