About lazy loading issues in EF Core 3.0
See original GitHub issueApplicationDbContext _context;
Cdkeys = this._context.Set<CdKey>();
KeyValues = this._context.Set<KeyValue>();
var keys = Cdkeys.Where(k => k.BuildType == (int)BuildKeyType.Test);
foreach(var key in keys)
{
var values = KeyValues.Where(v => v.KeyId == key.Id).ToList();//InvalidOperationException:
//There is already an open DataReader associated with this Command which must be closed first.
}
var keys = Cdkeys.Where(k => k.BuildType == (int)BuildKeyType.Test).ToList();//Because the loading is done
foreach(var key in keys)
{
var values = KeyValues.Where(v => v.KeyId == key.Id).ToList();//So,No error
}
- I don’t think this lazy loading is bad, instead I think it’s very cool.
- I hope the official can have some documentation about this, or help other through my example.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Lazy Loading of Related Data - EF Core
Lazy loading can cause unneeded extra database roundtrips to occur (the so-called N+1 problem), and care should be taken to avoid this. See...
Read more >Lazy Loading Related Data In Entity Framework Core
Lazy loading of data is a pattern whereby the retrieval of data from the database is deferred until it is needed.
Read more >About lazy loading issues in EF Core 3.0 #18358
This is not even related to lazy loading. You are trying to enumerate 2 queries at the same time. If your database provider...
Read more >Entity Framework Core - Lazy Loading
So writing my database models as I always have using the 'virtual' specifier to enable lazy loading for a List. Though when loading...
Read more >Avoid Lazy Loading in ASP.NET - Shawn Wildermuth
With Lazy Loading enabled, what is returned from the first query is a Proxy object. It's a temporary type that is derived from...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is not even related to lazy loading. You are trying to enumerate 2 queries at the same time. If your database provider does not support executing 2 DataReader at the same time, you will hit that error. If you are using SqlServer then enable MARS support. Else you cannot run 2 queries interleaved. You have to finish enumerating one query and then execute the other one.
Use foreach code as follows, still need to call ToList to fix the problem
https://stackoverflow.com/questions/2113498/sqlexception-from-entity-framework-new-transaction-is-not-allowed-because-ther
https://github.com/aspnet/EntityFrameworkCore/issues/10282