diff options
author | crupest <crupest@outlook.com> | 2019-12-10 22:19:37 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-12-10 22:19:37 +0800 |
commit | 7078b0c480ce0b8f44c23cd707826c2fb64b6202 (patch) | |
tree | 13dcdf360e18dd0719f76c3742c9467a1f51dcad | |
parent | e95030ec056577143f37c06de8351ba511e7ff75 (diff) | |
download | timeline-7078b0c480ce0b8f44c23cd707826c2fb64b6202.tar.gz timeline-7078b0c480ce0b8f44c23cd707826c2fb64b6202.tar.bz2 timeline-7078b0c480ce0b8f44c23cd707826c2fb64b6202.zip |
Fix a bug in simultaneous database queries.
Note on this:
You should not use Task.WhenAll to do a bunch of async database queries since EntityFramework Core does not support DbContext to do two queries in the same time.
-rw-r--r-- | Timeline/Services/TimelineService.cs | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index affcff2e..af4537af 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -349,13 +349,17 @@ namespace Timeline.Services var timelineId = await FindTimelineId(name);
var postEntities = await Database.TimelinePosts.OrderBy(p => p.Time).Where(p => p.TimelineId == timelineId && p.Content != null).ToListAsync();
- var posts = new List<TimelinePostInfo>(await Task.WhenAll(postEntities.Select(async p => new TimelinePostInfo
+ var posts = new List<TimelinePostInfo>();
+ foreach (var entity in postEntities)
{
- Id = p.Id,
- Content = p.Content,
- Author = (await Database.Users.Where(u => u.Id == p.AuthorId).Select(u => new { u.Name }).SingleAsync()).Name,
- Time = p.Time
- })));
+ posts.Add(new TimelinePostInfo
+ {
+ Id = entity.Id,
+ Content = entity.Content,
+ Author = (await Database.Users.Where(u => u.Id == entity.AuthorId).Select(u => new { u.Name }).SingleAsync()).Name,
+ Time = entity.Time
+ });
+ }
return posts;
}
|