I'm working on a code-first EF6 project using MySql. I have setup my data classes such that their related data objects should be lazy-loaded, but they seem to run very slowly each time they are called.
This is an example of one of my entity classes:
[Table("Carrier")]
public class DBCarrier
{
[Key]
[Column("CarrierId")]
public int carrierId { get; set; }
[MaxLength(128), Required]
public string CarrierName { get; set; }
public virtual ICollection<DBDepot> Depots { get; set; }
public virtual ICollection<DBZone> Zones { get; set; }
}
But when I call the Zones on an object of this class like this (this was all in one statement, but I separated it out to try and find out where the problem was):
ICollection<DBZone> zones = carrier.Zones;
IEnumerable<DBZone> zones1 = zones.Where(x => x.Postcode == postcode);
return zones.Select(x => x.ZoneName).FirstOrDefault();
Every time I call carrier.Zones it takes about 8 seconds to run. I thought that by defining zones as an ICollection would delay execution of the database query.