GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - darxis/EntityFramework.LazyLoading: LazyLoading for EF Core

BranchesTags
Open more actions menu

Latest commit

 

History

26 Commits
26 Commits

Folders and files

NameName
Last commit message
Last commit date
samples/Microsoft.EntityFrameworkCore.LazyLoading.Sample
samples/Microsoft.EntityFrameworkCore.LazyLoading.Sample
 
 
src/Microsoft.EntityFrameworkCore.LazyLoading
src/Microsoft.EntityFrameworkCore.LazyLoading
 
 
tests/Microsoft.EntityFrameworkCore.LazyLoading.Tests
tests/Microsoft.EntityFrameworkCore.LazyLoading.Tests
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
EntityFramework.LazyLoading.sln
EntityFramework.LazyLoading.sln
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 

Repository files navigation

EntityFramework.LazyLoading Build Status

Lazy Loading for EF Core

Inspired by and partially based on the blog post: https://weblogs.asp.net/ricardoperes/implementing-missing-features-in-entity-framework-core-part-6-lazy-loading

How to enable LazyLoading in EF Core?

Enabling LazyLoading in EF Core is extremely easy with this library. You just need to call UseLazyLoading() (see step 2 below).

However, you will need to slightly modify your entity classes, but just the References, not the Collections (see step 3 below).

Step 1 - Nuget package

Reference the Microsoft.EntityFrameworkCore.LazyLoading NuGet package (https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.LazyLoading/).

Step 2 - Configure the DbContext builder

Call UseLazyLoading() on the DbContextOptionsBuilder when creating the DbContext.

public class MyDbContextFactory : IDbContextFactory<MyDbContext>
{
    private readonly bool _isLazy;

    public MyDbContextFactory (bool isLazy)
    {
        _isLazy = isLazy;
    }

    public MyDbContext Create(DbContextFactoryOptions options)
    {
        var dbContextOptionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
        dbContextOptionsBuilder.UseSqlServer("<some_connection_string>");

        // Here we enable LazyLoading
        dbContextOptionsBuilder.UseLazyLoading();

        return new MyDbContext(dbContextOptionsBuilder.Options);
    }
}

Step 3 - Adjust the model

In your model you need to declare References using the type LazyReference<T>. Collections don't require additional configuration in your model, just use the ICollection<> type.

public class Parent
{
    public ICollection<Child> Childs { get; set; }
}

public class Child
{
    private LazyReference<Parent> _parentLazy = new LazyReference<Parent>();
    public Parent Parent
    {
        get
        {
            return _parentLazy.GetValue(this);
        }
        set
        {
            _parentLazy.SetValue(value);
        }
    }
}

Step 4 - Done!

That's all, LazyLoading enabled! It was so easy, wasn't it?

About

LazyLoading for EF Core

Topics

dbcontextef-coreefcoreentity-frameworkentity-framework-corelazyloadingorm

Resources

Readme
MIT license
Activity

Stars

21 stars

Watchers

4 watching

Forks

4 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.