Error with multiple open data readers for child entity
I have a ef 4 model and I'm using self tracking entities. In开发者_高级运维 this model there is an entity called Organisation. Each Organisation can have many Locations (addresses). If I try to select a single location (so I can delete it), thus:
var location = _container.Locations.FirstOrDefault(l => l.Id == id);
I get an erorr that there is already a data reader open so I cant open another. If I do the following:
var location = _container.Locations.Include("Organisation").FirstOrDefault(l => l.Id == id);
Then it all works just fine.
Using Intellitrace I can see that with the failed query it executes an ADO command to get just the location then does another command to get the location and the organisation.
Is this a bug or something I need to do differently for selftracking entities?
No it is not a bug. This usually happens if you iterate result of one query and in this iteration you execute another query (it can also happen due to lazy loading but lazy loading is not supported with self tracked entities). The simplest solution is simply allowing multiple active data readers (your database must support it). In case of SQL Server 2005 and newer you can simply add to your connection string this part: MultipleActiveResultSets=true;
精彩评论