ASP。NET MVC WebApi & EF延迟加载=永久加载?

时间:2023-01-16 09:30:07

In my ASP.NET MVC WebApi project I have a Repository (with an Interface) that queries the DB via a simple Linq SingleOrDefault command and then outputs everything to The Controller. (EF = Database first against Oracle DB)

在我的ASP。NET MVC WebApi项目我有一个存储库(带有接口),它通过一个简单的Linq SingleOrDefault命令查询DB,然后将所有内容输出到控制器。(EF =数据库优先针对Oracle DB)

The Problem is that, when I turn on Lazy Loading (via the EDMX file property) the get is loading forever and there is no response. If I turn off Lazy Loading everything works fine?

问题是,当我打开延迟加载(通过EDMX文件属性)时,get一直在加载,没有响应。如果我关闭延迟加载,一切正常吗?

Even stranger ist that when I put all the query in a console app and output everything via Console.WriteLine it works also with lazy loading?

甚至更奇怪的是,当我把所有的查询放到一个控制台应用程序中并通过控制台输出所有内容时。是否也适用于延迟加载?

Interface:

接口:

Public Interface IMedienRepository
    Function [Get](id As Integer) As MEDIEN
End Interface

Repository

存储库

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

Controller

控制器

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

FYI: It isn't working even when I put the query directly into the controller!

提示:即使我将查询直接放入控制器,它也不能工作!

I would be grateful if anybody knows about this issue and could help me because I like to use the Lazy loading feature in my MVC Views.

如果有人知道这个问题并能帮助我,我将非常感激,因为我喜欢在MVC视图中使用惰性加载特性。

My work around for the Moment is to turn Lazy Loading off (db.ContextOptions.LazyLoadingEnabled = False) for calls via the WebApi ...

我目前的工作是关闭延迟加载(db. context toptions)。LazyLoadingEnabled = False)通过WebApi调用…

1 个解决方案

#1


1  

Given the behavior you describe, I suspect the lazy loading is causing (for some reason) the loading of a LOT of data from the database, which takes a long time and looks like no response. Turning lazy loading off ensures that only the first "level" of your query is returned, without populating any collection properties.

考虑到您所描述的行为,我怀疑延迟加载导致(出于某种原因)从数据库中加载大量数据,这需要很长时间,而且看起来没有响应。关闭延迟加载可以确保返回查询的第一个“级别”,而不会填充任何集合属性。

If these calls are for a WebAPI, I'm not sure you would get any real benefit from lazy loading. API calls should be very deterministic in the way they query the database and return strictly defined result sets. Writing a well-performing API is hard (just ask the StackOverlow team!) and query performance is critical.

如果这些调用是针对WebAPI的,我不确定您是否会从延迟加载中获得任何真正的好处。API调用在查询数据库和返回严格定义的结果集的方式上应该非常确定。编写一个性能良好的API是很困难的(只需要问StackOverlow团队!)和查询性能是至关重要的。

#1


1  

Given the behavior you describe, I suspect the lazy loading is causing (for some reason) the loading of a LOT of data from the database, which takes a long time and looks like no response. Turning lazy loading off ensures that only the first "level" of your query is returned, without populating any collection properties.

考虑到您所描述的行为,我怀疑延迟加载导致(出于某种原因)从数据库中加载大量数据,这需要很长时间,而且看起来没有响应。关闭延迟加载可以确保返回查询的第一个“级别”,而不会填充任何集合属性。

If these calls are for a WebAPI, I'm not sure you would get any real benefit from lazy loading. API calls should be very deterministic in the way they query the database and return strictly defined result sets. Writing a well-performing API is hard (just ask the StackOverlow team!) and query performance is critical.

如果这些调用是针对WebAPI的,我不确定您是否会从延迟加载中获得任何真正的好处。API调用在查询数据库和返回严格定义的结果集的方式上应该非常确定。编写一个性能良好的API是很困难的(只需要问StackOverlow团队!)和查询性能是至关重要的。