【发布时间】:2012-08-18 00:16:39
【问题描述】:
在我的 ASP.NET MVC WebApi 项目中,我有一个存储库(带有接口),它通过一个简单的 Linq SingleOrDefault 命令查询数据库,然后将所有内容输出到控制器。 (EF = 数据库优先针对 Oracle DB)
问题是,当我打开延迟加载(通过 EDMX 文件属性)时,get 将永远加载并且没有响应。如果我关闭延迟加载一切正常?
更奇怪的是,当我将所有查询放在控制台应用程序中并通过 Console.WriteLine 输出所有内容时,它也适用于延迟加载?
界面:
Public Interface IMedienRepository
Function [Get](id As Integer) As MEDIEN
End Interface
存储库
Private db As EFEntities
Public Sub New()
db = New EFEntities
End Sub
Public Function [Get](id As Integer) As MEDIEN Implements IMedienRepository.[Get]
Dim _medien = db.MEDIEN.SingleOrDefault(Function(m) m.MEDIENNR = id)
If _medien Is Nothing Then
Throw New NotFoundException()
End If
Return _medien.SingleOrDefault()
End Function
控制器
Private _repository As IMedienRepository
Public Sub New()
Me.New(New MedienRepository())
End Sub
Public Sub New(repository As IMedienRepository)
_repository = repository
End Sub
Public Function GetValue(id As Integer) As MEDIEN
Try
Return _repository.Get(id)
Catch generatedExceptionName As NotFoundException
Throw New HttpResponseException(New HttpResponseMessage() With { _
.StatusCode = System.Net.HttpStatusCode.NotFound _
})
End Try
End Function
仅供参考:即使我将查询直接放入控制器也无法正常工作!
如果有人知道这个问题并可以帮助我,我将不胜感激,因为我喜欢在我的 MVC 视图中使用延迟加载功能。
我目前的工作是关闭延迟加载 (db.ContextOptions.LazyLoadingEnabled = False) 以通过 WebApi 调用...
【问题讨论】:
-
你能把
MEDIEN这个类型的定义贴出来吗?您使用哪个客户端进行 GET(AJAX、fiddler 等)? -
MEDIEN 是我的 Oracle DB 中的一个数据库表,其中包含各种列和数据类型。我用过所有主流浏览器和fiddler来调用API,结果都一样。
标签: asp.net-mvc linq entity-framework asp.net-web-api