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
Table of Contents
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | private void findByID() { using (ChinookContext db = new ChinookContext()) { var Employee = db.Employee.Find(1); if (Employee !=null) { Console.WriteLine("{0} {1} Email @ {2}", Employee.FirstName, Employee.LastName, Employee.Email); } } Console.WriteLine("Press any key to continue"); Console.ReadKey(); } |
The SQL Query of the above method
1 2 3 4 5 6 7 8 | //The Following query is generated by EF Code SELECT TOP(1) [e].[EmployeeId], [e].[Address], [e].[BirthDate], [e].[City], [e].[Country], [e].[Email], [e].[Fax], [e].[FirstName], [e].[HireDate], [e].[LastName], [e].[Phone], [e].[PostalCode], [e].[ReportsTo], [e].[State], [e].[Title] FROM [Employee] AS [e] WHERE [e].[EmployeeId] = @__p_0 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | private void findByCompositeID() { using (ChinookContext db = new ChinookContext()) { var playlistTrack = db.PlaylistTrack.Find(1,1); if (playlistTrack != null) { Console.WriteLine("{0} {1} ", playlistTrack.PlaylistId, playlistTrack.TrackId); } } Console.WriteLine("Press any key to continue"); Console.ReadKey(); } |
SQL Query
1 2 3 4 5 6 | //Query SELECT TOP(1) [p].[PlaylistId], [p].[TrackId] FROM [PlaylistTrack] AS [p] WHERE ([p].[PlaylistId] = @__p_0) AND ([p].[TrackId] = @__p_1) |
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.
1 2 3 4 5 6 7 8 9 10 | using (ChinookContext db = new ChinookContext()) { //Query is sent to the database and entity is added to the context var Employee1 = db.Employee.Find(1); //Here the query is not sent to the database. It returns the entity from the context var Employee2 = db.Employee.Find(1); } |
Notes on find
- 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.
- If find fails to find the entity in step 1, then the EFCoew will query send the query database to fetch the records
- 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
Find | Single, First |
---|---|
Checks the context first for the record, else sends the query to database | Always sends the query to database |
You can query only on primary key | Query can be on any fields using where clause |
Returns null if record not found | Single & First method raised exception. SingleOrDefault & FirstOrDefault returns null |
cc