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)
我是否遗漏了某些内容,或者无法异步读取数据库中的内容?如果是这样,我很好奇只是阅读内容的根本不同之处在于,允许异步执行此操作并不合适?
因为First
只是不是异步的.尝试FirstAsync
改为:
string works = (await dbContext.Chatlogs.OrderBy(msg => msg.time).FirstAsync()).content;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
50 次 |
最近记录: |