Entity Framework - Eager loading related entities

后端 未结 4 1356
野趣味
野趣味 2021-02-13 19:17

I\'m using Entity Framework 4 with MVC and need to ensure any referenced entities I want to use in my view have been loaded before the controller method returns, otherwise the v

4条回答
  •  攒了一身酷
    2021-02-13 20:14

    I'm in the same situation. I think that with EF is very easy to fall in a 101 query problem.

    A solution can be to create a partial class of your Seller class (generated by EF) and implement a GetSubclassNameQ that return a IQueryable, and a GetSubclassNameQFull that return a IQueryable with eager loading.

    public partial class Seller{
    
      public IQueryable GetRecommendationsQ(EntityContainer entitycontainer) {
        return entitycontainer.Recommendations;
      }      
    
      public IQueryable GetRecommendationsQFull(EntityContainer entitycontainer) {
        return this.GetRecommendationsQ(entitycontainer).Include("Recommendations.User");
      }
    
      public IQueryable GetRecommendationsQ() {
        return GetRecommendationsQ(new EntityContainer());
      }
    
      public IQueryable GetRecommendationsQFull() {
        return this.GetRecommendationsQ().Include("Recommendations.User");
      }
    
    }
    

提交回复
热议问题