question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

About lazy loading issues in EF Core 3.0

See original GitHub issue
ApplicationDbContext _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
}
  1. I don’t think this lazy loading is bad, instead I think it’s very cool.
  2. I hope the official can have some documentation about this, or help other through my example.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
smitpatelcommented, Oct 15, 2019

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.

0reactions
JohnHe404commented, Oct 17, 2019

Use foreach code as follows, still need to call ToList to fix the problem

var keys = Cdkeys.Where(k => k.BuildType == (int)BuildKeyType.Test);
 foreach(var key in keys)
 {
var keyvalue = new KeyValue
{
   Id = ....
  KeyType = keyId,
};
var values = KeyValues.Insert();
//SqlException from Entity Framework - New transaction is not allowed because there are other threads running in the session
}

https://stackoverflow.com/questions/2113498/sqlexception-from-entity-framework-new-transaction-is-not-allowed-because-ther

https://github.com/aspnet/EntityFrameworkCore/issues/10282

var keys = Cdkeys.Where(k => k.BuildType == (int)BuildKeyType.Test).ToList();
 foreach(var key in keys)
 {
var keyvalue = new KeyValue
{
   Id = ....
  KeyType = keyId,
};
var values = KeyValues.Insert();
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found