我正在尝试向google.api发送一些asnyc请求,如果我运行这个经典foreach它工作正常没有任何问题,但当我用parallel.foreach替换它有时不会跳进去,如果因为
keyword.AdGroup在if子句中没有任何内容。当我进入parallel.foreach之前我看关键字列表时,所有都有Adgroup和Campaign子对象延迟加载。但是后来在foreach内部它以某种方式失去了广告组,因为它变得一无所获。这与使用多线程的实体框架有关吗?
Dim myDB As New DBContext
Dim keywords As List(Of Keyword) = myDB.Keyword.ToList()
Parallel.ForEach(keywords, New ParallelOptions With {.MaxDegreeOfParallelism = 10}, Sub(keyword As Keyword)
If keyword IsNot Nothing AndAlso _
keyword.AdGroup IsNot Nothing AndAlso keyword.AdGroup.Campaign IsNot Nothing AndAlso keyword.AdGroup.Campaign.CustomerId.HasValue Then
updateStatus(keyword.AdGroup.Campaign.CustomerId, keyword.AdGroupId, keyword.KeywordId, UserStatus.PAUSED)
Else
End If
编辑: 我在这里发现了类似的问题,但我不清楚答案。有人能说明一个例子吗?
Entity Framework lazy loading doesn't work from other thread