为什么修改数据库数据的查询是等待的,而只读取数据的查询不是?

gaa*_*kam 1 c# asynchronous async-await entity-framework-core

我可以异步地将新行写入数据库:

await dbContext.Chatlogs.AddAsync(new ChatMessage(messageString, time, author));
await dbContext.SaveChangesAsync();
Run Code Online (Sandbox Code Playgroud)

但是只是从数据库中读取内容呢?

string firstMessageEverPosted = dbContext.Chatlogs.OrderBy(msg => msg.time).First().content;
Run Code Online (Sandbox Code Playgroud)

不,await这里 并且插入await会导致编译错误:

string doesntWork = await dbContext.Chatlogs.OrderBy(msg => msg.time).First().content;
Run Code Online (Sandbox Code Playgroud)

我是否遗漏了某些内容,或者无法异步读取数据库中的内容?如果是这样,我很好奇只是阅读内容的根本不同之处在于,允许异步执行此操作并不合适?

Hau*_*ger 6

因为First只是不是异步的.尝试FirstAsync改为:

string works = (await dbContext.Chatlogs.OrderBy(msg => msg.time).FirstAsync()).content;
Run Code Online (Sandbox Code Playgroud)