I am a newbie to Entity Framework and would like some clarification on some things. I understand the DbContext
such as pointing to a SQL database with a connection string. Within the DbContext
, you declare DbSet<perEachTable>
that you want / plan to expose and get data from, from said SQL database.
Now, you get into LINQ, and I understand some of link such as enumerable lists of things and looking for certain qualifying where condition entries.
Now, my bigger question and how EF queries. You have a DbContext
pointing to SQL Server. It has a table of say 250k customers. It has its primary key defined as Id
(or CustomerId
) which is expected. You want to query and lookup a customer based on the user's email address (common type of expected query). The customer table has an index on the email.
In the LINQ queries I have seen, it is referring to the DbSet
of whatever table and runs a where clause, such as
using (var context = new DBCustomers())
{
var query = context.Customer
.Where(c => c.EMail == "someEMail@whereEver.com")
.FirstOrDefault<Customer>();
}
My mental thinking is that the entire list of customers is being pulled down from its DbSet context. Then it runs the link WHERE
clause to look for email. I don't think I want to be pulling down 250k customers down every time just to LINQ iterate through them.
How is it that the WHERE
does not actually use the entire dataset of customers (especially as/when it continues to grow) and just optimizes based the indexes available and does NOT actually pull everything down.
Is this just a magic black-box being applied and EF just hands you the entry (or few if other more open query) the final entries that qualified.
I have also seen instances of EF Core using fully written SQL statements and parameterized which is what I am more accustomed to doing. Letting the SQL Server engine return the data based on the explicit criteria that best matches qualifying indexes.
Appreciate clarification on the underlying operations.