'How to use lazy loading with view models

I am creating an MVC ASP.Net application and have a large list of data that I need to display in a table, hoewever, not all data is in one table of my database. Because of this I have created a view model to get all necessary data and put it in one location for my view. This has worked perfectly until recently when I have begun to get large amounts of data. Now the process of moving all data from models to the view model takes too long. Traditionally I would use lazy loading to cut down on this time, but am unsure how to do this with a view model. Is this even possible?

If there is any code that you would like, please let me know, but I'm not sure what code would be necessary right now.



Solution 1:[1]

From the other answer posted the solution seems to be for your database to:

  var.Resource.Include(s=>s.someResource);

The Include() applies eager loading. Eager loading is the process where a query for one type of entity also loads related entities as part of the query. This will speed it up so you do not make many requests.

Solution 2:[2]

Put this in your DBContext class and install the microsoft.proxis package:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseLazyLoadingProxies().UseSqlServer("Data Source=.;Initial Catalog=Da2;Integrated Security=True");
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pipeline
Solution 2 Jeremy Caney