ASP.NET MVC的WebAPI和放大器; EF延迟加载=永远加载? [英] ASP.NET MVC WebApi & EF Lazy Loading = forever loading?

查看:114
本文介绍了ASP.NET MVC的WebAPI和放大器; EF延迟加载=永远加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET MVC的WebAPI项目,我有一个库(与接口),它通过一个简单的LINQ的SingleOrDefault命令查询数据库,然后输出一切控制器。 (EF =数据库第一对Oracle数据库)

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)

问题是,当我打开延迟加载(通过EDMX文件属性)中得到的是永远加载并没有任何反应。如果我关闭延迟加载一切正常?

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?

更奇怪的IST,当我把所有的查询在一个控制台应用程序和输出都通过Console.WriteLine它的工作原理也与延迟加载?

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?

接口:

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

FYI:当我把查询直接进入控制器它甚至没有工作

如果有人知道关于这个问题,可以帮助我,因为我喜欢使用懒加载功能在我的MVC意见我将不胜感激。

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.

我的工作围绕的时刻是将延迟加载关闭( db.ContextOptions.LazyLoadingEnabled =假)通过调用的WebAPI ...

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

推荐答案

由于您所描述的行为,我怀疑懒加载造成(由于某种原因),从数据库中,这需要很长的数据有很多的负荷时间,看起来像没有任何反应。谈到延迟加载了确保只有您的查询的第一个级别返回,没有任何填充集合属性。

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.

如果这些调用的的WebAPI,我不知道你会得到延迟加载任何实际的好处。 API调用应该是他们查询数据库,并返回严格定义的结果集的方式非常确定的。编写一个性能良好的API是硬(只是要求StackOverlow队!)和查询性能是至关重要的。

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.

这篇关于ASP.NET MVC的WebAPI和放大器; EF延迟加载=永远加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆