Skip to content

Commit fea068f

Browse files
committed
Detach completed tasks after update
1 parent c54f3a2 commit fea068f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

RequestTests.paw

642 Bytes
Binary file not shown.

src/SimpleToDoService/Database/Repositiory.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,16 @@ public Task CreateTask(Task task)
9393

9494
var entity = context.Tasks.Add(task);
9595

96-
foreach (var tsk in context.Tasks.Where(o => o.TaskPrototypeUuid == task.TaskPrototypeUuid && o.Completed == false))
97-
{
98-
tsk.Completed = true;
99-
}
96+
var tasksToComplete = context.Tasks.Where(o => o.TaskPrototypeUuid == task.TaskPrototypeUuid && o.Completed == false).ToList();
97+
tasksToComplete.ForEach(o => o.Completed = true);
10098

10199
try
102100
{
103101
if (context.SaveChanges() > 0)
102+
{
103+
tasksToComplete.ForEach(o => context.Entry(o).State = EntityState.Detached);
104104
return entity.Entity;
105+
}
105106
}
106107
catch(DbUpdateException ex)
107108
{
@@ -115,14 +116,18 @@ public Task CreateTask(Task task)
115116
entity = context.Tasks.Add(task);
116117

117118
if (context.SaveChanges() > 0)
119+
{
120+
tasksToComplete.ForEach(o => context.Entry(o).State = EntityState.Detached);
118121
return entity.Entity;
122+
}
119123
}
120124
else
121125
{
122126
throw;
123127
}
124128
}
125129

130+
tasksToComplete.ForEach(o => context.Entry(o).State = EntityState.Detached);
126131
return null;
127132
}
128133

0 commit comments

Comments
 (0)