Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First lazy loading non navigation properties

I'm using the entity framework code first CTP4.

Is it possible to lazy load non navigation properties like you can in NH 3.

A common example would be having a table containing a binary column. I only want to retrieve this column's data when I explicitly ask for that property in my code e.g. image.ImageData

Thanks Ben

like image 828
Ben Foster Avatar asked Oct 31 '10 11:10

Ben Foster


People also ask

How do I enable lazy loading code first in Entity Framework?

Lazy loading means delaying the loading of related data, until you specifically request for it. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.

What is lazy loading in code first approach?

Lazy loading is delaying the loading of related data, until you specifically request for it. It is the opposite of eager loading. For example, the Student entity contains the StudentAddress entity.

Does Entity Framework support lazy loading?

Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading.


1 Answers

  1. Vote here
  2. Vote here
  3. Read this
  4. Ugly workaround:

    public static void Main()
    {
      IEnumerable<MyTable> table;
      using (Entities context = new Entities())
      {
        var buffer =
          context.MyTable
          .Select(myTable => new
          {
            Id = myTable.Id,
            OtherColumn = myTable.OtherColumn
          })
          .ToArray();
    
        table = buffer
          .Select(t => new MyTable 
          {
            Id = t.Id, 
            OtherColumn = t.OtherColumn
          });
      }
    }
    

This will not select the rest of the fields.

like image 75
Shimmy Weitzhandler Avatar answered Oct 10 '22 16:10

Shimmy Weitzhandler