I can asynchronously write new rows to the database:
await dbContext.Chatlogs.AddAsync(new ChatMessage(messageString, time, author));
await dbContext.SaveChangesAsync();
But what about just reading content from the databse?
string firstMessageEverPosted = dbContext.Chatlogs.OrderBy(msg => msg.time).First().content;
No await
here. And inserting await
causes compilation errors:
string doesntWork = await dbContext.Chatlogs.OrderBy(msg => msg.time).First().content;
Am I missing something or is it not possible to asynchronously read content from the database? If so, I'm curious what is so fundamentally different with just reading content that it was not deemed useful to allow doing this asynchronously?
Because First
just is not asynchronous. Try FirstAsync
instead:
string works = (await dbContext.Chatlogs.OrderBy(msg => msg.time).FirstAsync()).content;