none
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
    {
    
        [Column("VISITDATE")]
        [RequiredDateTimeAttribute(ErrorMessage = "Date can't be blank")]
        public DateTime VisitDate { get; set; }
    
        [Column("STATUS")]
        public long Status { get; set; }
    
        [Column("OWNERID")]
        public long OwnerId { get; set; }
    
        [Column("COORDID")]
        public long? CoordinatorId { get; set; }
    
        [Column("REPORTID")]
        public long ReportId { get; set; }
    
        [Column("ASSETID")]
        [Range(1, long.MaxValue, ErrorMessage = "Asset can't be blank")]
        public long AssetId { get; set; }
    
        [Column("ASSETTYPE")]
        [MaxLength(50)]
        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
    {
        [Column("TYPENAME")]
        [MaxLength(60)]
        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;
        } 
    
        [Column("CREATEDAT")]
        public DateTime CreatedAt { get; set; }
        [Column("UPDATEDAT")]
        public DateTime UpdatedAt { get; set; } 
    }

    Then we have the reviewships and crewships models:

    public class Reviewship : ModelTrackingBase
    {
        [Column("USERID")]
        public long UserId { get; set; }
    
        [Column("REPORTID")]
        public long ReportId { get; set; }
    
        [Column("NOTIFYADDED")]
        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

Answers

  • Hello Rex200789,

    For your encountered scenario, you could check this link:

    https://msdn.microsoft.com/en-us/data/hh949853.aspx?f=255&MSPPError=-2147217396

    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.

    Regards.


    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.

    Wednesday, August 12, 2015 9:36 AM
    Moderator