I am trying to debug performance problems with an older MVC application, using EF 5 (4.4). SQL Server Profiler reveals way more queries are being ran than should be. EF appears to be generating queries for every related entity to the model in question.
Now this sounds like lazy-loading. The thing is, no navigation properties were referenced anywhere in code (at least not explicitly).
When I turn lazy loading off for the context, it fixes it. Also, if I return something other than the model (such as a viewmodel), it fixes it. This is an old, poorly implemented MVC app - and it should be using viewmodels anyway, so that's fine. Either of these are acceptable fixes, but I still want to know why it's happening.
I have read that navigation properties may get invoked when the object is serialized. Is that what's happening here? If so, can you explain why the object is being serialized? (Noting that my understanding of serialization is very basic - basically what is said here.)
Here's an example:
Controller
[HttpGet]
public ViewResult StoreInfo(int id)
{
Store model = _posRepository.GetStore(id);
return View(model);
}
Repository
public Store GetStore(int storeID)
{
return _dbContext.Store.Single(x => x.StoreID == storeID);
}
Model
public partial class Store // highly simplified version
{
public int StoreID { get; set; }
public string StoreName { get; set; }
public Nullable<int> StateID { get; set; }
public virtual States State { get; set; } // lazy loaded
}
View
// blank