Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have done some research about this, but no success. Sorry if duplicate or dumb question.

Is there any way to disable lazy loading for specific query on Entity Framework 6? I want to use it regularly, but sometimes I want to disable it. I'm using virtual properties to lazy load them.

Thanks in advance.

Best regards, Marco Alves.

share|improve this question
12  
set context.Configuration.LazyLoadingEnabled = false; before the query you want to run – Karthik Ganesan Jun 3 '14 at 19:06
3  
You could just set the value this.Configuration.LazyLoadingEnabled = false;, then set it again this.Configuration.LazyLoadingEnabled = true;? Also, you can read this msdn.microsoft.com/en-us/data/jj574232.aspx – user1477388 Jun 3 '14 at 19:07
    
thank you @KarthikGanesan. It worked as expected. – Marco Alves Jun 3 '14 at 20:40
    
@KarthikGanesan Can you put your comment as an answer ? It's working really well :) – Sampath Jun 6 at 8:13
1  
Added the comment as answer @Sampath – Karthik Ganesan Jun 6 at 21:59

You can disable Lazy loading for specific query as follows :

public static Cursos GetDatosCursoById(int cursoId)
{
    using (var bd = new AcademyEntities())
    {
        try
        {
            bd.Configuration.ProxyCreationEnabled = false;
            return bd.Cursos.FirstOrDefault(c => c.cursoId == cursoId);
        }
        catch (Exception ex)
        {
            return null;
        }
    }
}
share|improve this answer

set the following code before the query you want to execute

context.Configuration.LazyLoadingEnabled = false;
share|improve this answer
    
Yes,This is the easiest way and it works.Thanks :) – Sampath Jun 7 at 1:25

Go to your diagram properties and find a property designated to lazy loading and disable it.

If you are using code first then go to your config area and disable it from there with:

this.Configuration.LazyLoadingEnabled = false;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.