Entity Framework Selecting All Rows

There is one thing that confuses me. EF selects all rows. All records in the table. Let me show you a sample.

    public Category GetByID(int Id)
    {
        return context.Categories.Find(Id);
    }

      

there are many records in the table, and when I check them with a breakpoint, I can see all records not only with the number given in the ID. What if the table contains 10 thousand records? I am checking it out. I have copied all records manually in the database and I am making 30K records.

An expression like this

IEnumerable<Category> categories = categoryRepository.Where(x => x.Published == true).ToList();

      

I saw 30 thousand records with a break point. But atleast 10k Published False in the table.

Does the core Entity first fetch all the entries into memory and then filter them?

+3


source to share


1 answer


The short answer is no, as long as the Entity Framework can parse the expression IQueryable<>

(which includes the predicates .Where

you specify), it converts the linked expression tree to native Sql using the appropriate query provider for the RDBMS you are targeting, which allows for all the benefits of Sql, like using indexes.

As per my comment, one of the common reasons EF will not do this is because the mechanism has IQueryable

been tampered with, for example if the repository pattern implementation uses a IEnumerable<T>

predicate overloadWhere

rather than an IQueryable overload .



As a result, EF has no other option but to retrieve the table and execute each row against your predicate function to determine if the row matches your predicate or not.

As an aside, there is some discussion about whether there is merit in the DbContext wrapper in the repository and / or the Unit Of Work wrapper, since a DbContext

is Transactional, does caching, can be mocked during unit testing, and now supports a wide range of databases.

+2


source







All Articles