EF Core Find Method

EF Core Find method finds a record with the given primary key values. If the entity is already in the context (because of a previous query), then the Find method returns it. The Find method sends the query to the database if it does not find it in the context.

Database:
The Database for this tutorial is taken from the chinook database.
Source Code:
The source code of this project available in GitHub. It also contains the script of the database

Find Method

Finding the row with the Primary Key is one of the common tasks that we perform on a table. The Find method of the DbSet property of the DbContext allows us to quickly query the database. It uses the Primary Key to return the matching row. It returns null if no matching rows are found.

Find by Primary Key ID

In the following example db.Employee.Find(1) send the query where EmployeeId=1 to the database

The SQL Query of the above method

Find By Composite Key

Finding Entities with Composite Primary Keys is equally easy. Composite Key is consists of more than one field (or property) of the table. The chinook database table, PlaylistTrack has a composite primary key. PlaylistId & TrackId.

This table can be queried using the Find method as shown below.

The Find method uses the Primary Key to construct the SQL query and sends it to the database to retrieve the data. The order in which you specify the parameter for the Find method must match the order of the primary key defined in the database.

SQL Query

Find does not send query to database always

The Find Method always looks for the Entity in the context first. If it does not find the entity in the context, then the EF Core will send the query to the database.

In the following example, we are querying for an employee with EmployeeId=1. The EF Core sends the first query to the database. But EF Core will not send the subsequent query to the database but uses the result which is still present in the context.

Notes on find

  1. Find searches within the context to locate the entity with the given Primary/Composite Key. It will also look for the entities which are added in the context but not saved in the database.
  2. If find fails to find the entity in step 1, then the EFCoew will query send the query database to fetch the records
  3. If it fails to find the entity, then it will return NULL

Find Vs Single (Also Vs SingleOrDefault , First & FirstOrDefault)

It appears that the find and Single (or SingleOrDefault, First & FirstOrDefault) methods are similar. But they do have differences

FindSingle, First
Checks the context first for the record, else sends the query to databaseAlways sends the query to database
You can query only on primary keyQuery can be on any fields using where clause
Returns null if record not foundSingle & First method raised exception.

SingleOrDefault & FirstOrDefault returns null

References

1 thought on “EF Core Find Method”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top