我使用存储库和实体框架来插入任务及其依赖关系和可交付类型的任务..... 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);
}
}