/// <summary> /// A method to create a WorkOrder, along with associated WorkAssignments and WorkerRequests, in the database. /// </summary> /// <param name="workOrder">The work order to be created.</param> /// <param name="workerRequestList">A list of worker requests made by the employer.</param> /// <param name="username">User performing action</param> /// <param name="workAssignments">A collection representing the worker assignments for the work order.</param> /// <returns>The created WorkOrder.</returns> public WorkOrder Create(WorkOrder workOrder, List <WorkerRequest> workerRequestList, string username, ICollection <WorkAssignment> workAssignments = null) { workOrder.timeZoneOffset = Convert.ToDouble(cfg.getConfig(Cfg.TimeZoneDifferenceFromPacific)); updateComputedValues(ref workOrder); workOrder.createdByUser(username); var createdWorkOrder = repo.Add(workOrder); createdWorkOrder.workerRequestsDDD = new Collection <WorkerRequest>(); if (workerRequestList != null) { foreach (var workerRequest in workerRequestList) { workerRequest.workOrder = createdWorkOrder; workerRequest.workerRequested = wServ.Get(workerRequest.WorkerID); workerRequest.updatedByUser(username); workerRequest.createdByUser(username); createdWorkOrder.workerRequestsDDD.Add(workerRequest); } } uow.SaveChanges(); if (createdWorkOrder.paperOrderNum == null || createdWorkOrder.paperOrderNum == 0) { createdWorkOrder.paperOrderNum = createdWorkOrder.ID; } if (workAssignments != null) { foreach (var workAssignment in workAssignments) { workAssignment.ID = default(int); //so EF Core will save the record; otherwise IDENTITY_INSERT fails workAssignment.workOrderID = createdWorkOrder.ID; workAssignment.workOrder = createdWorkOrder; waServ.Create(workAssignment, username); } } uow.SaveChanges(); _log(workOrder.ID, username, "WorkOrder created"); return(createdWorkOrder); }