爲什麼下面的代碼總是產生「異步模塊或處理程序在異步操作仍未完成時完成」?使用異步時EF IdentityDbContext出現問題方法
當我使用'Dim OriginalUser As ApplicationUser = db.Users.Where(Function(p)p.Id = id).ToList(0)'它工作正常。
延遲加載在從IdentityDbContext繼承的ApplicationDBContext上被禁用。爲什麼SaveChanges-Part沒有可用的上下文?我錯過了什麼?
Public Async Sub PatchUser(id As String, <FromBody> ChangedUserAttributes As Delta(Of ApplicationUser))
Using ctx As New ApplicationDbContext
Validate(ChangedUserAttributes.GetEntity())
If Not ModelState.IsValid Then
Throw New HttpResponseException(New HttpResponseMessage(HttpStatusCode.BadRequest))
End If
Dim OriginalUser As ApplicationUser = Await ctx.Users.SingleOrDefaultAsync(Function(p) p.Id = id)
If OriginalUser Is Nothing Then
Throw New HttpResponseException(HttpStatusCode.NotFound)
End If
Try
ChangedUserAttributes.TrySetPropertyValue("Email", "Emil")
ChangedUserAttributes.Patch(OriginalUser)
Await ctx.SaveChangesAsync
Return
Catch ex As Exception
Throw New HttpResponseException(New HttpResponseMessage(HttpStatusCode.BadRequest))
End Try
End Using
End Sub
它看起來像是一個控制器動作,所以它不會被直接調用,但是對於「Async Function ... As Task」而不是「Async Sub」你當然是正確的。 – jmcilhinney
謝謝,這已經有所幫助。我使用了一個Sub,因爲在WEB API的PATCH動作中,根據Microsoft的結果應該是204(無內容)(https://www.asp.net/web-api/overview/getting-started-with-aspnet-網上API /行動結果)。但是當我嘗試返回返回新的HttpResponseMessage(HttpStatusCode.NoContent)時,我得到一個'無法轉換'System.Net.Http.HttpResponseMessage'類型的對象來鍵入'System.Web.Http.IHttpActionResult'錯誤。 – user3767106
@ user3767106:你應該只返回一個普通的'Task',而不是*任務*。 –