コード例 #1
0
        public WorkItem AddWorkItem(WorkItem workItem)
        {
            WorkItem retVal = new WorkItem();
            using (var db = new CoWorkerDbContext())
            {
                try
                {
                    db.WorkItems.Add(workItem);
                    db.SaveChanges();

                    long id = workItem.WorkItemId;

                    if (id > 0)
                    {
                        var result = db.WorkItems.Find(new object[] { id, workItem.Title });
                        retVal = result as WorkItem;
                    }
                    else
                    {
                        throw new FaultException("WorkItem was not saved.");
                    }
                }
                catch (Exception e)
                {
                }
            }

            return retVal;
        }
コード例 #2
0
        public WorkItem UpdateWorkItem(WorkItem workItem)
        {
            WorkItem retVal = new WorkItem();
            using (var db = new CoWorkerDbContext())
            {
                try
                {
                    db.WorkItems.Attach(workItem);
                    var workitemEntry = db.Entry(workItem);
                    workitemEntry.State = System.Data.Entity.EntityState.Modified;

                    //todo: study if there is a better way to update child tables.
                    var feedBacks = db.Feedbacks;

                    var strengthToUpdate = feedBacks.FirstOrDefault(f => f.FeedbackId == workItem.Strength.FeedbackId);
                    strengthToUpdate.Comment = workItem.Strength.Comment;
                    db.Entry(strengthToUpdate).State = System.Data.Entity.EntityState.Modified;

                    var areaImprovementToUpdate = feedBacks.FirstOrDefault(f => f.FeedbackId == workItem.AreaForImprovement.FeedbackId);
                    areaImprovementToUpdate.Comment = workItem.AreaForImprovement.Comment;
                    db.Entry(areaImprovementToUpdate).State = System.Data.Entity.EntityState.Modified;

                    db.SaveChanges();

                    long id = workItem.WorkItemId;

                    if (id > 0)
                    {
                        var result = db.WorkItems.Find(new object[] { id, workItem.Title });
                        retVal = result as WorkItem;
                    }
                    else
                    {
                        throw new FaultException("WorkItem was not saved.");
                    }
                }
                catch (Exception e)
                {
                    throw new FaultException("WorkItem was not saved.");
                }
            }

            return retVal;
        }