Tuesday, 6 August 2013

Entity Framework loads excessive data

Entity Framework loads excessive data

On previous efforts with old versions of Entity Framework, I'm used to
using Eager Loading - so you get your root entity and then gather related
entities as required by the use of "Include".
On my current project we've implemented the latest version of EF on a new
database, using database-first. Take this class, for example:
public partial class Zone
{
public Zone()
{
this.OverrideCharges = new HashSet<OverrideCharge>();
}
public System.Guid RowId { get; set; }
public string Name { get; set; }
public virtual ICollection<OverrideCharge> OverrideCharges { get; set; }
}
The OverrideCharges object also has a number of sub-properties underneath
it, with related entities under those.
We have two contexts, the actual DB context and a set of DTO contexts. The
objects for the latter are mostly copies of the former - the Zone_dto
object is pretty much a clone of the original. In both, I have turned off
Lazy Loading by using:
Configuration.LazyLoadingEnabled = false;
Now, I query my Zone objects by doing this:
public List<Zone_dto> GetZones()
{
List<Zone> zones = _cilContext.Zones.ToList();
List<Zone_dto> zone_dtos = new List<Zone_dto>();
foreach (Zone zn in zones)
{
zone_dtos.Add(Mapper.Map<Zone, Zone_dto>(zn));
}
return zone_dtos;
}
So - no includes. And Lazy Loading is disabled. I would expect to get back
a list of Zone objects and their direct properties, but not any related
entities.
But what I get back are the Zone objects, plus all their OverrideCharges,
plus all the related entities to those OverrideCharges and so on, all the
way down the tree.
These data objects are not huge, and it's not a massive problem. But I'm
frustrated that I don't understand why I'm getting back all this data that
I haven't asked for. Can someone explain?

No comments:

Post a Comment