Lazy Loading is Missing in Entity Framework 7

I thought to shar  yet another of my experiences with Entity Framework 7 where Lazy Loading is still not implemented. I was actually designing my Movie-Review API and soon after that I realized that results are unexpected, hence without wasting time, I directly checked with Microsoft. Here is my conversation with Microsoft.
 
 
 
 Therefore, below is the glimpse of the snippet, which was expected to work and the workaround which I have written to give the expected results.
 
  1. using Microsoft.AspNet.Mvc;  
  2. using MovieReviewSPA.Data.Contracts;  
  3. using MovieReviewSPA.Model;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using MovieReviewSPA.Web.ViewModels.Movie;  
  8. using Microsoft.Data.Entity;  
  9.   
  10. namespace MovieReviewSPA.Web.Controllers.API  
  11. {  
  12.     [Route("api/[controller]")]  
  13.     public class MoviesController : Controller  
  14.     {  
  15.         private IMovieReviewUow UOW;  
  16.   
  17.         public MoviesController(IMovieReviewUow uow)  
  18.         {  
  19.             UOW = uow;  
  20.         }  
  21.   
  22.         //TO DO:- Lazy loading is still not implemented in EF 7  
  23.         //Refer here https://github.com/aspnet/EntityFramework/issues/3312  
  24.         //Once, same get implemented, Below query will work without any issue  
  25.         // GET api/movies  
  26.         [HttpGet("")]  
  27.         public IQueryable Get()  
  28.         {  
  29.             /*var model = UOW.Movies.GetAll().OrderByDescending(m => m.Reviews.Count()) 
  30.                .Select(m => new MovieViewModel 
  31.                { 
  32.                    Id = m.Id, 
  33.                    MovieName = m.MovieName, 
  34.                    DirectorName = m.DirectorName, 
  35.                    ReleaseYear = m.ReleaseYear, 
  36.                    NoOfReviews = m.Reviews.Count() 
  37.                });*/  
  38.   
  39.             //This is workaround for lazy loading  
  40.             var model = UOW.Movies.GetAll().Include(x => x.Reviews).OrderByDescending(m => m.Reviews.Count)  
  41.                 .Select(m => new MovieViewModel  
  42.                 {  
  43.                     Id = m.Id,  
  44.                     MovieName = m.MovieName,  
  45.                     DirectorName = m.DirectorName,  
  46.                     ReleaseYear = m.ReleaseYear,  
  47.                     NoOfReviews = m.Reviews.Count  
  48.                 });  
  49.   
  50.             return model;  
  51.         }  
  52.   
  53.         // Update an existing movie  
  54.         // PUT /api/movie/  
  55.         [HttpPut("")]  
  56.         public HttpResponseMessage Put([FromBody]Movie movie)  
  57.         {  
  58.             UOW.Movies.Update(movie);  
  59.             UOW.Commit();  
  60.             return new HttpResponseMessage(HttpStatusCode.NoContent);  
  61.         }  
  62.   
  63.         // Create a new movie  
  64.         // POST /api/movies  
  65.         [HttpPost("")]  
  66.         public int Post([FromBody]Movie movie)  
  67.         {  
  68.             UOW.Movies.Add(movie);  
  69.             UOW.Commit();  
  70.             return Response.StatusCode = (int)HttpStatusCode.Created;  
  71.         }  
  72.   
  73.   
  74.         // DELETE api/movies/5  
  75.         [HttpDelete("{id}")]  
  76.         public HttpResponseMessage Delete(int id)  
  77.         {  
  78.             UOW.Movies.Delete(id);  
  79.             UOW.Commit();  
  80.             return new HttpResponseMessage(HttpStatusCode.NoContent);  
  81.         }  
  82.   
  83.     }  

 With this change in place, it produced the expected result as shown below.
 
 
Here, you can see that it stared giving me reviews count as well, without lazy loading it was giving 0 always.
 
Thanks,
Rahul Sahay
Happy Coding 
Ebook Download
View all
Learn
View all