fcatae/Arda

[Kanban] WorkloadRepository.EditWorkload deletes technology, metrics and users

Closed this issue · 2 comments

If EditWorkload receives an EntityFramework model instead of a simple model, then the technology, metrics and users are deleted.

The problem is in this logic:

            _context.Files.RemoveRange(files);
            _context.WorkloadBacklogMetrics.RemoveRange(metrics);
            _context.WorkloadBacklogTechnologies.RemoveRange(technologies);
            _context.WorkloadBacklogUsers.RemoveRange(users);
            _context.SaveChanges();

This deletes the data prior to inserting the data

            var metricList = new List<WorkloadBacklogMetric>();
            foreach (var mId in workload.WBMetrics)
            {
                var metric = _context.Metrics.First(m => m.MetricID == mId);
                metricList.Add(new WorkloadBacklogMetric()
                {
                    Metric = metric
                });
            }

The problem is that when the workload is an EF model, then there is no data to insert it later.

It looks like the bug was fixed after upgrading EF.

Since we have no clear repro, I am adding these lines:

            var assertWBTechnologies = (workload.WBTechnologies == null) ? 0 : workload.WBTechnologies.Count();
            var assertWBUsers = (workload.WBUsers == null) ? 0 : workload.WBUsers.Count();
            var assertWBMetrics = (workload.WBMetrics == null) ? 0 : workload.WBMetrics.Count();

            if (checkWBTechnologies != assertWBTechnologies)
                throw new InvalidOperationException("Bug #44: collections changed");

            if (checkWBUsers != assertWBUsers)
                throw new InvalidOperationException("Bug #44: collections changed");

            if (checkWBMetrics != assertWBMetrics)
                throw new InvalidOperationException("Bug #44: collections changed");

But I believe it is fixed. The scenario is very specific and impacts only when EditWorkload is called after AddWorkload using the same object.