dotnet / dotnet/efcore

Disable injection of LazyLoader in some situations

Open
#33,400 10 comments 0 reactions 0 assignees View on GitHub
area-dbcontext area-proxies area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Hello,

var ctx = new DbContextSG(options);
ctx.ChangeTracker.AutoDetectChangesEnabled = false;

// should this disabled the creation of LazyLoader() ?
ctx.ChangeTracker.LazyLoadingEnabled = false;

public class Customer {

public DbContext DbContext { get; set; } // has only minor performance impact

public ILazyLoader LazyLoader { get; set; } // has Huge performance impact

}


for (int i = 0; i < 100_000; i++)
{
var c1 = new Customer();
ctx.Customers.Add(c1); // attaches entitiy to the context in state "Added", also creates and injects all Services

// side note: `ctx.AddRangeAsync()` does not do a batch action, it's slow and Synchronous, the name Async is confusing.

}

A few things i've noticed: Injecting the ILazyLoader is slow.
Around 30% slower than not having the "ILazyLoader LazyLoader" property in my customer object.
The 100k loop takes: 1300ms vs 1000ms without "ILazyLoader LazyLoader".

The LazyLoader is created and injected for new Entities.
The LazyLoader is created and injected for all Entities of all Queries.
The LazyLoader is created and injected if "ChangeTracker.LazyLoadingEnabled = false"

Is this the desired behaviour?

I feel like we can save some resources here if we don't create and inject 100k of LazyLoader() that are not needed.

> Are LazyLoader objects ever needed for new entities?
> I would love to disable the creation of LazyLoader objects if i know that i don't need LazyLoaders for a query.

Best Regards

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.