0

I have an entity model generated as below:

public partial class Entity
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Entity()
    {
        this.Comments = new HashSet<Comment>();
        this.Fields = new HashSet<FieldEntity>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public int AssigneeID { get; set; }
    public string Description { get; set; }
    public System.DateTime Created { get; set; }
    public int CreatedBy { get; set; }
    public System.DateTime LastEdited { get; set; }
    public int LastEditBy { get; set; }
    public bool Deleted { get; set; }
    public Nullable<System.DateTime> DeletedDate { get; set; }

    public virtual UserEntity Assignee { get; set; }
    public virtual UserEntity CreatedByUser { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Comment> Comments { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<FieldEntity> Fields { get; set; }
    public virtual UserEntity LastEditByUser { get; set; }
}

When I use EF LinqToSql to fetch this entity, for some reason the list of Comments and Fields are populated even though I don't use Include() or Load() as specified here.

This is a sample query I use:

var entities = ctx.MyEntities.Single(x => x.ID == 1);

I thought the default behavior was to lazily load related entities, so I'm trying to figure out if something is wrong with my query or my model.

For what it's worth, I did search my solution for LazyLoadingEnabled and it is supposed to be enabled:

//from MyDBModel.edmx
<EntityContainer Name="MyEntities" annotation:LazyLoadingEnabled="true">
...
Michael
  • 85
  • 1
  • 7
  • 1
    How did you come to the conclusion that they are loaded immediately and not when you access the collection/relationship? – Igor Jan 14 '16 at 01:22
  • @Igor if I put a breakpoint after running that query, I can inspect `entities` and it has the collections populated. I was under the impression that they would be null or otherwise empty without explicitly including the related entities. Is there a better way to verify if they are actually lazily loaded? – Michael Jan 14 '16 at 01:27
  • @Sami I tried that just now, and the debug inspection looks the same as with `Single`. – Michael Jan 14 '16 at 01:29
  • I just corrected a typo with the second call.. But now I began to doubt my self anyhow. Removing the comment.. – Sami Jan 14 '16 at 01:30
  • @Igor you were right, they are being lazily loaded. It's just that when I inspect through the debugger, the related entities are then loaded rather than some sort of null or exception being generated. – Michael Jan 14 '16 at 01:51

1 Answers1

0

Lazy Loading is enabled by default in EF. If you want to disable it, you can check the answer to this another question related with the same topic.

Also, you can confirm if your code is performing the Lazy Loading using the SQL Server Profiler tool. Check this link for more information about how to do that.

Community
  • 1
  • 1
Hernan Guzman
  • 1,217
  • 8
  • 14