From f19dc927d773a86e9385c38d07a6e239f4f6c5fa Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 10 Dec 2019 22:19:37 +0800 Subject: 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. --- Timeline/Services/TimelineService.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'Timeline/Services') 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(await Task.WhenAll(postEntities.Select(async p => new TimelinePostInfo + var posts = new List(); + 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; } -- cgit v1.2.3