Entity framework Eager and Lazy load

c# eager-loading entity-framework entity-framework-6 lazy-loading

Question

Understanding the distinction between eager and lazy loading in entity framework. Consider the models I have:

public interface IBaseEntityObject 
{
    public int Id {get; set;}
}


public abstract class BaseEntityObject : IBaseEntityObject
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get; set;}
}


public class Folder : BaseEntityObject
{   
    [DataMember]
    public string Name {get; set;}

    [DataMember]
    public virtual List<Letter> Letters {get; set;} 
}


public abstract class Letter : BaseEntityObject
{   
    [DataMember]
    public string Title {get; set;}

    [DataMember]
    public string Content {get; set;}

    public virtual Folder Folder {get; set;}

    [DataMember]
    public int FolderId {get; set;}

    [DataMember]
    public DateTime CreationDate {get; set;}
}

public class OutgoingLetter : Letter
{
    // .. OutgoingLetter properties
}

public class ReceviedLetter : Letter
{
    // .. ReceviedLetter properties
}


public class MyDbContext : DbContext
{
    public DbSet<Folder> Folders {get; set;}

    public DbSet<Letter> Letters {get; set;}


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // Folder <-> Letters       
        modelBuilder.Entity<Letter>()
        .HasRequired(t => t.Folder)
        .WithMany(f => f.Letters)
        .HasForeignKey(t => t.FolderId)
        .WillCascadeOnDelete(true);
    }
}

The primary function of every folder I request from the database is to access the Letters contained within it. So, if I access the Letters property, will it be best if I already load it from the beginning rather than doing another database call?

I've heard that turning off the lazy load requires setting the EnableLazyLoading property to false and deleting the virtual key word from the letters list from the ctor.

What would happen if I kept the Lazy loading enabled and simply used Include(x => x.Lettters) whenever I asked for a folder? When lazy loading is turned on, include cannot be utilized.

Is there a connection between the Letter model's Folder attribute and lazy loading as well? I don't utilize the Folder when asking for a letter, but the Father property is present in one-to-many relationships in most models I've observed.

Thanks!

1
2
2/19/2016 12:42:39 PM

Accepted Answer

ZZZ_tmp
2
2/19/2016 12:48:54 PM

Popular Answer

Include is also used with lazy load turned on. I typically only use Include when I have performance difficulties because it makes the code more readable.
Additionally, navigation may alter throughout the course of a project, so slow load eliminates the requirement for constant memory.
Additionally, using a lazy load during serialization will likely result in serializing the entire database.

In addition, Include frequently creates really ugly queries only for information (but I never had performance issues).

Parent characteristics are solely helpful for navigation (i.e. you can navigate from a Letter to a Folder then to the Letters of the Folder).



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow