插入的行采用相同的值

时间:2018-04-30 17:24:27

标签: .net asp.net-mvc asp.net-web-api c#-4.0 entity-framework-6

我使用存储库和实体框架来插入任务及其依赖关系和可交付类型的任务..... item.Depens int值列表。

问题是newdependicies中的数据列出DependingOnTaskID取值TaskID

public IHttpActionResult Add(TaskViewModel item)
{
    try
    {
        var _item = new Task { Name = item.Title,Description  = item.Descrip,JobID=item.jopId ,};
        _unitOfWork.TaskRepository.Add(_item);
        _unitOfWork.TaskRepository.Commit();
        var newDeliverables = new List<TaskDeliverable>();
        foreach (var i in item.Delivs)
        {
           var existingTask = _unitOfWork.TaskRepository.FindBy(i);
            if (existingTask != null)
            {
                var dl = new TaskDeliverable() { DeliveredToTaskD=i ,TaskID = _item.ID };
                newDeliverables.Add(dl);
            }
        }
        var newdependicies = new List<TaskDependency>();

        foreach (var i in item.Depens)
        {
            var existingTask = _unitOfWork.TaskRepository.FindBy(i);
            if (existingTask != null)
            {
                var de = new TaskDependency() { DependingOnTaskID=i ,TaskID=_item.ID};
                newdependicies.Add(de);
            }
        }
        _item.TaskDependencies = newdependicies;
        _item.TaskDeliverables = newDeliverables;
        _unitOfWork.TaskRepository.Update(_item.ID, _item);
        _unitOfWork.TaskRepository.Commit();
        item.ID = _item.ID;
        return Ok(item);
    }
    catch (Exception ex)
    {
        string errorId = ex.LogElmah();
        var message = Utilities.WebApiError(1, Common.Constants.SystemErrorMessage, errorId);
        return BadRequest(message);
    }
}

0 个答案:

没有答案