2

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)

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?

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.

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

2
  • Can you post the definition of the type MEDIEN? Which client do you use for the GET (AJAX, fiddler, etc.) ? Commented Aug 22, 2012 at 14:39
  • MEDIEN is a database table in my Oracle DB with various columns and datatypes. I've used all major browsers and fiddler to call the API, all with the same result. Commented Aug 22, 2012 at 15:14

1 Answer 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.

Sign up to request clarification or add additional context in comments.

3 Comments

I also see no real benefit in using lazy loading with the Web Api, but it really helps in the MVC parts especially when dealing with master / detail scenarios. Do you have any advice how to switch off the feature for all API repositories, but not for the ones used in the MVC application? (FFunny thig is still that the call via an console app is blazing fast ...)
You should be able to set the lazy loading options per repository instance, so that the MVC app enables it but WebAPI calls disable it. Have you debugged/profiled the execution with lazy loading enabled when called from through WebAPI?
Seems like I need to stick with disabling the lazy loading in the New constructor of every WebApi repository! (BTW: I stepped it through a couple of times and it seems to have problems with the navigation propertiers/foreign keys as you suggested.) Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.