The Find
method looks in the DbContext for an entity which has the specified key(s). If there is no matching entity already loaded, the DbContext will makes a SELECT TOP 1
query to get the entity.
Running db.Students.Where(s => s.StudentID == id)
will get you a sequence containing all the entities returned from a SQL query similar to SELECT * FROM Students WHERE StudentID = @id
. That should be pretty fast; you can speed it up by using db.Students.FirstOrDefault(s => s.StudentID == id)
, which adds a TOP 1
to the SQL query.
Using Find
is more efficient if you're loading the same entity more than once from the same DbContext. Other than that Find
and FirstOrDefault
are pretty much equivalent.
In neither case does the context load the entire table, nor does it hold open a connection. I believe the DbContext holds a connection until the DbContext is disposed, but it opens and closes the connection on demand when it needs to resolve a query.