Caz1224
Caz1224

Reputation: 1569

Entity Framework Core - Lazy Loading

Bowing to my Visual Studios request, I started my latest project using Entity Framework Core (1.0.1)

So writing my database models as I always have using the 'virtual' specifier to enable lazy loading for a List. Though when loading the parent table it appears that the child list never loads.

Parent Model

public class Events
{
    [Key]

    public int EventID { get; set; }
    public string EventName { get; set; }
    public virtual List<EventInclusions> EventInclusions { get; set; }
}

Child Model

public class EventInclusions
{
    [Key]
    public int EventIncSubID { get; set; }
    public string InclusionName { get; set; }
    public string InclusionDesc { get; set; }
    public Boolean InclusionActive { get; set; }

}

Adding new records to these tables seems to work as I am used to where I can nest the EventInclusions records as a List inside the Events record.

Though when I query this table

_context.Events.Where(e => e.EventName == "Test")

The Issue

EventInclusions will return a null value regardless of the data behind the scenes.

After reading a bit I am getting the feeling this is a change between EF6 which I normally use and EF Core

I could use some help in either making a blanket Lazy Loading on statement or figuring out the new format for specifying Lazy Loading.

Caz

Upvotes: 53

Views: 81560

Answers (7)

pejman
pejman

Reputation: 798

you can instaling this package for enable lazy loading in EF Core 2.1.

Microsoft.EntityFrameworkCore.Proxies

and then set this config in your ef dbContext

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
     => optionsBuilder
           .UseLazyLoadingProxies()
           .UseSqlServer("myConnectionString");

"Notice" this package works only on EF Core 2.1 and above.

Upvotes: 33

Kowi
Kowi

Reputation: 231

For EF Core 2.1 and above,

Install:

 dotnet add package Microsoft.EntityFrameworkCore.Proxies --version 2.2.4 

Then Update your Startup.cs file as indicated below.

using Microsoft.EntityFrameworkCore.Proxies;



services.AddEntityFrameworkProxies();
services.AddDbContext<BlogDbContext>(options =>
            {
                options.UseSqlite(Configuration.GetSection("ConnectionStrings")["DefaultConnection"]);
                options.UseLazyLoadingProxies(true);
            });

Upvotes: 12

Elijah Lofgren
Elijah Lofgren

Reputation: 1477

Lazy loading is now available on EF Core 2.1 and here is link to the relevant docs:

https://learn.microsoft.com/en-us/ef/core/querying/related-data#lazy-loading

Upvotes: 41

Eric
Eric

Reputation: 2283

There's a pre-release version that just came out, regardless it's supposed to be available in full release soon.

A couple of caveats:

  • All your data properties that are more than simple types (ie: any other classes/tables) need to be public virtuals (default scaffolding they're not).
  • This line gets tucked into OnConfiguring on your data context:

        optionsBuilder.UseLazyLoadingProxies();
    
  • It's (currently) pre-release so may the force be with you.

Upvotes: 7

Caz1224
Caz1224

Reputation: 1569

So it appears that EF Core does not currently support lazy loading. Its coming but may be a while off.

For now if anyone else comes across this problem and is struggling. Below is a demo of using Eager loading which is what for now you have to use.

Say before you had a person object and that object contained a List of Hats in another table.

Rather than writing

var person = _context.Person.Where(p=> p.id == id).ToList();

person.Hats.Where(h=> h.id == hat).ToList();

You need to write

var person = _context.Person.Include(p=> p.Hats).Where(p=> p.id == id).ToList();

And then person.Hats.Where(h=> h.id == hat).ToList(); will work

If you have multiple Lists - Chain the Includes

var person = _context.Person.Include(p=> p.Hats).Include(p=> p.Tickets)
                            .Include(p=> p.Smiles).Where(p=> p.id == id).ToList();

I kinda get why this method is safer, that your not loading huge data sets that could slow things down. But I hope they get Lazy loading back soon!!!

Caz

Upvotes: 37

baHI
baHI

Reputation: 1570

Lazy load is planned to be in EF core 2.1 - you can read more on why it is a must-have feature - here.

Upvotes: 3

Darxis
Darxis

Reputation: 1570

LazyLoading is not yet supported by EF Core, but there is a non-official library that enables LazyLoading: https://github.com/darxis/EntityFramework.LazyLoading. You can use it until it is officially supported. It supports EF Core v1.1.1. It is available as a nuget package: https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.LazyLoading/

Disclaimer: I am the owner of this repo and invite you to try it out, report issues and/or contribute.

Upvotes: 5

Related Questions