EF 5 data loading issue. RRS feed

  • Question

  • There has been an issue I have been facing with entity framework. I have inherited a project which I have to enhance and I am facing a weird issue. The first time the visit model is accessed and when the virtual property Reports is accessed, it takes like a minute to run. After that any subsequent times this property is called, it is fast(1-1.5 seconds). The models in question are as below:

    //Visit model
    public class Visit : ModelTrackingBase
        [RequiredDateTimeAttribute(ErrorMessage = "Date can't be blank")]
        public DateTime VisitDate { get; set; }
        public long Status { get; set; }
        public long OwnerId { get; set; }
        public long? CoordinatorId { get; set; }
        public long ReportId { get; set; }
        [Range(1, long.MaxValue, ErrorMessage = "Asset can't be blank")]
        public long AssetId { get; set; }
        public string AssetType { get; set; }
        public virtual Report Report { get; set; }//this is the one that causes the lag

    Then we have the model reports which is as follows:

    //model reports
    public abstract class Report : ModelTrackingBase
        public string TypeName { get; set; } 
        public virtual ICollection<Crewship> Crewships { get; set; }
        public IEnumerable<User> KeyPersonnel
            get { return Crewships.Select(c => c.User); }
        public virtual ICollection<Reviewship> Reviewships { get; set; }
        public IEnumerable<User> ReviewTeam
            get { return Reviewships.Select(r => r.User); }

    I will also show the subsequent models that the code gets data from:

    public class ModelTrackingBase : EntityIdBase
        protected ModelTrackingBase()
            CreatedAt = UpdatedAt = DateTime.Now;
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; } 

    Then we have the reviewships and crewships models:

    public class Reviewship : ModelTrackingBase
        public long UserId { get; set; }
        public long ReportId { get; set; }
        public DateTime? NotifyAdded { get; set; } 
        public virtual User User { get; set; }
        public virtual Report Report { get; set; }
    Crewships is same except the notifyadded property. It takes a minute to access the property Visits.Report whenever that model is called to data-bind on the view-model, any subsequent calls are 1-1.5 second long. Any idea on what I am doing wrong here?

    Monday, August 10, 2015 7:11 PM


  • Hello Rex200789,

    For your encountered scenario, you could check this link:


    As we can see, for first time call, Entity Framework would build a cold query and for second time and later, it uses the cached query so that it would be faster. And Entity Framework would use a content to cache its queried data.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Tuesday, August 18, 2015 1:35 AM
    Wednesday, August 12, 2015 9:36 AM