Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6938893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:33:01+00:00 2026-05-27T12:33:01+00:00

I have a repository class which is querying the EF DbContext like this: public

  • 0

I have a repository class which is querying the EF DbContext like this:

public IEnumerable<ProductFilterData> GetAllProducts()
{
   return this.DataContext.Products.Select(x => new ProductFilterData { Id = x.Id, Name = x.Name, ProductDetailLevelCode = x.ProductDetailLevel.Code, Description = x.Description, ParentProductIds = x.ParentProducts.Select(p => p.Id) });
}

Which works fine. However, when I refactor this code to this:

public IEnumerable<ProductFilterData> GetAllProducts()
{
    return this.MapToProductFilterData(this.DataContext.Products);
}

private IEnumerable<ProductFilterData> MapToProductFilterData(IEnumerable<Product> products)
{
    return products.Select(x => new ProductFilterData { Id = x.Id, Name = x.Name, ProductDetailLevelCode = x.ProductDetailLevel.Code, Description = x.Description, ParentProductIds = x.ParentProducts.Select(p => p.Id) });
}

I get an exception:

There is already an open DataReader associated with this Command which
must be closed first.

Why does passing a reference to the DbSet to a method cause a change in the behavior of EF? Incidentally, if I change the parameter type of MapToProductFilterData to IQueryable then it works. Also, if I remove the relationship mappings it also works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:33:01+00:00Added an answer on May 27, 2026 at 12:33 pm

    You must change it to IQueryable because if you pass it as IEnumerable you actually execute query SELECT * FROM Product. Your method starts iterating the result set but your projection calls x.ParentProducts.Select(p => p.Id) which results in lazy loading of ParentProducts navigation property. Lazy loading opens second active data reader which is possible only if you have it enabled in connection string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have repository class in asp.net mvc which has this, public Material GetMaterial(int id)
First, I have a dbcontext factory which is defined public class DatabaseFactory : Disposable,
I have a repository Class which takes in a ObjectContext called TestDB. I when
I have a Linq-To-Sql based repository class which I have been successfully using. I
I have a Repository Class with the following method... public T Single<T>(Predicate<T> expression) {
I have a repository class that inherits from a generic implementation: public namespace RepositoryImplementation
Example I have a repository class (DAL): public class MyRepository : IMyRepository { public
I have the following Repository: Public Class PageRepository Private Shared _pages As BLL.PageCollection Shared
I have two generic save methods in a repository class: public void Save<T>(T entity)
I have repository class that is used by at least 2 other classes. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.