Base solution for your next web application
Open Closed

How to persist data to database after Repository.Insert #1035


User avatar
0
feliw created

Hi,

Any advice on how to persist data directly after Repository insert or udpate ? My problem right now is, I need to use LINQ to join with the newly inserted data. But because the data is not persisted to DB until the process is finished so my LINQ query return null for me.

A bit of my code below, so the idea is under 1 process, I insert the 'Style' data, and then use it on the LINQ query on the //insert production schedule part

//insert style
                var styles = (from tbInput in input.AsQueryable()
                              select new { Code = tbInput.StyleCode, Description = tbInput.StyleDescription })
                          .GroupBy(c => new { c.Code, c.Description })
                          .Select(rs => new CreateStyleDto
                          {
                              Code = rs.FirstOrDefault().Code,
                              Description = rs.FirstOrDefault().Description,
                          }).ToList();

                foreach (CreateStyleDto dto in styles)
                {
                    CreateOrUpdateStyle(dto);
                }

                //insert production schedule
                var productionschedule = (from tbInput in input.AsEnumerable()
                                          join tbProductionLine in _productionLineRepository.GetAll().AsEnumerable() on tbInput.ProductionLineFRCode equals tbProductionLine.FastReactCode
                                          join tbStyle in _styleRepository.GetAll().AsEnumerable() on tbInput.StyleCode equals tbStyle.Code
                                          select new
                                          {
                                              Facility = tbInput.Facility,
                                              ProductionLineId = tbProductionLine.Id,
                                              StyleId = tbStyle.Id,
                                              StyleCode = tbInput.StyleCode,
                                              StyleDescription = tbInput.StyleDescription,
                                              SAM = tbInput.SAM,
                                              ProductionNo = tbInput.ProductionNo,
                                              ExFactory = tbInput.ExFactory,
                                              NoOfOperators = tbInput.NoOfOperators,
                                              NoOfWorkHours = tbInput.NoOfWorkHours,
                                              RemainingQty = tbInput.RemainingQty,
                                              ActualQty = tbInput.ActualQty,
                                              PlanDate = tbInput.PlanDate,
                                          }).GroupBy(grouper => new
                                          {
                                              grouper.Facility,
                                              grouper.ProductionLineId,
                                              grouper.StyleId,
                                              grouper.SAM,
                                              grouper.ExFactory,
                                              grouper.NoOfOperators,
                                              grouper.NoOfWorkHours,
                                              grouper.PlanDate
                                          }).
                               Select(rs => new CreateProductionScheduleDto
                               {
                                   Facility = rs.FirstOrDefault().Facility,
                                   StyleId = rs.FirstOrDefault().StyleId,
                                   ProductionLineId = rs.FirstOrDefault().ProductionLineId,
                                   SAM = rs.FirstOrDefault().SAM,
                                   ExFactory = rs.FirstOrDefault().ExFactory,
                                   InlineDate = new DateTime(),
                                   NoOfOperators = rs.FirstOrDefault().NoOfOperators,
                                   NoOfWorkHours = rs.FirstOrDefault().NoOfWorkHours,
                                   PlanDate = rs.FirstOrDefault().PlanDate,
                                   ActualQty = rs.Sum(c => c.ActualQty),
                                   RemainingQty = rs.Sum(c => c.RemainingQty)
                               }).ToList();

                foreach (CreateProductionScheduleDto dto in productionschedule)
                {
                    dto.InlineDate = GetInlineDate(dto);
                    ProductionScheduleDto currentDto = CheckExist(dto);
                    if (currentDto == null)
                    {
                        _productionScheduleRepository.Insert(dto.MapTo<ProductionSchedule>());
                    }
                    else
                    {
                        currentDto.ActualQty = dto.ActualQty;
                        currentDto.NoOfOperators = dto.NoOfOperators;
                        currentDto.NoOfWorkHours = dto.NoOfWorkHours;
                        currentDto.RemainingQty = dto.RemainingQty;
                        currentDto.SAM = dto.SAM;
                        currentDto.InlineDate = dto.InlineDate;
                        _productionScheduleRepository.Update(currentDto.MapTo<ProductionSchedule>());
                    }
                }

Thanks before :D :D


2 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can use savechanges of current unit of work if your code runs in an app service or abp controller.

    CurrentUnitOfWork.SaveChanges();
    
  • User Avatar
    0
    feliw created

    Hii Thanks .. Great support, solved my problem already..