locked
Lazy loading related question RRS feed

  • Question

  • Hi,

    I am using EF 4.1 and I ran into an issue related with lazy loading.

    I have a class named Directory that owns a one to many relationship self reference (a directory can have sub directories). My directory also has an identifier (primary key identity) and a path (string) as attributes.

    If I execute ''DbSet<Directory>().Find(1)'' to retrieve a directory by its primary key, sub directories are not loaded. This is the behavior I expect. I want to be able to explicitly load sub directories when needed.

    If in other hand I try to make a query that use a predicate to retrieve a specific directory ex: (from DbSet<Directory>() select d).Where(d => d.Path == "/dir1").FirstOrDefault() the subdirectories are loaded eagerly. I want to be able to execute queries with predicates that do not load automatically children collections.

     

    Can someone help me?

    Michael Pinheiro

    Thursday, May 26, 2011 1:23 AM

Answers

All replies

  • I detect my problem but I am not sure is this the correct way to do what I want.

     

    My Find method via primary key:

        public TE Find<TE>(params object[] keyValues) where TE : StoredEntity
        {
          try
          {
            var e = Set<TE>().Find(keyValues);
            if (e != null)
              Detach(e);
            return e;
          }
          catch (Exception e)
          {
            throw new StoreException("Occorreu um erro ao seleccionar a entidade", e);
          }
    
        }
    

    My Find method via lambda expression:

        public TE Find<TE>(Expression<Func<TE, bool>> filterExpression) where TE : StoredEntity
        {
          try
          {
            var entity = (from e in Set<TE>() select e).Where(filterExpression).FirstOrDefault();        
    <strong>        if (entity != null)
              Detach(entity);</strong>
            return entity;
          }
          catch (Exception e)
          {
            throw new StoreException("Occorreu um erro ao seleccionar a entidade", e);
          }
        }
    

     

    In this last method I was not detaching the selected entity. It seems that detach (Detach is a method that calls Detach from the context) reset children collection.

     

    Is it the best way to do that? I want my entities to be loaded without children collection and only load collections when I need them.

     

     

     

    Thursday, May 26, 2011 9:35 AM
  • Hi Mickael83,

    Thank you for posting.

     

    From your post, it looks that you want to load entity, but not child entities according to your needs. Here’s my suggestion. Hope this helps.

    You can check RelatedEnd.IsLoaded property. To guarantee that a related end is fully loaded before doing processing, you should check whether IsLoaded is false. If it’s false, you should call the Load method.

    Here’s an article about How to implicit lazy loading when it was generated by the EDM. Please check here. http://www.singingeels.com/Articles/Entity_Framework_and_Lazy_Loading.aspx

     

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, May 28, 2011 4:07 PM
  • Any update? Would you mind letting us know how it goes? Was your question solved?

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 2, 2011 11:03 AM