Updated February 14, 2023
Introduction to Entity Framework Find
Entity Framework Find method finds the record with the specified primary fundamental values. Then, the finding method sends the queries to the database to retrieve the records; if already the documents are available in the context, they will find from the context itself is no need to rescue them from the database; it will get from the context itself.
Overview of Entity Framework Find
The specified primary fundamental values find the records in the Entity Framework Find method. This Find method returns the record from the context if it is present already; if there is no relevant record present in the context, then the Find method sends the queries to the database.
To Find an entity with the specified primary fundamental values as follows:
- It will return directly without requesting the database if an entity with specified key values exists in the context.
- Or else the request will be raised to the database for the entity with specified primary fundamental values; once an entity is found, it is then attached to the context and returned.
- If no entity is found in the context of the database, then it will return NULL.
Look at the following example to find a record:
Code:
using (var dbContext = new BookDB())
{
var getAuthors = dbContext.AuthorDetails.Find(2);
}
It will find within the context to search the entity with the given Primary Key or Composite Key. It also searches out the entities included in the context but not saved in the database. If the Find ignores finding the entity in the previous step, then the Entity Framework will send a query to the database to retrieve the records. If there are no records found, then it will return NULL.
How to Use Entity Framework Find Work?
Entity Framework Find Method: The entities or records are searched based on the specific primary key value used to build the SQL Queries. Once it finds the records, it returns them to the context. Then, it arranges the documents based on finding and orders the key defined in the database. Finding the records based on the Primary Key is the common task we perform on the table. The Find Method of the DbSet Property of DbContext enables querying the database. It will use the Primary Key to return the matching records; if no matching record is found, it returns NULL.
Code:
using System;
using System.Data.Entity;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
public class EF_FindProgram
{
public static void Main()
{
InsertingDetails();
using (var dbContext = new BookDB())
{
var getAuthor = dbContext.Authors
.Find(2);
Console.WriteLine(getAuthor.Author_FirstName + " " + getAuthor.Author_LastName);
}
}
public static void InsertingDetails()
{
using (var dbContext = new BookDB())
{
var Authors = new List<AuthorDetail>
{
new AuthorDetail() {Author_FirstName = "Peter", Author_LastName = "Paul"},
new AuthorDetail() {Author_FirstName = "David", Author_LastName = "Jim"},
new AuthorDetail() {Author_FirstName = "Sarav", Author_LastName = "Bravo"},
new AuthorDetail() {Author_FirstName = "Mark", Author_LastName = "Henry"},
new AuthorDetail() {Author_FirstName = "Rio", Author_LastName = "San"}
};
var Books = new List<BookDetail>
{
new BookDetail() { Book_Title = "C# Programming", Author_Id = 1},
new BookDetail() { Book_Title = "Java Guide", Author_Id = 1},
new BookDetail() { Book_Title = "SQL Beginners Guide", Author_Id = 2},
new BookDetail() { Book_Title = "Tutorials in ASP.NET", Author_Id = 3},
new BookDetail() { Book_Title = "ANDROID Absolute Beginners", Author_Id = 3}
};
dbContext.Authors.AddRange(Authors);
dbContext.Books.AddRange(Books);
dbContext.SaveChanges();
}
}
}
Let’s see the following Entity Classes and DbContext codes.
It is the entity classes for Book details and the Author details as follows:
Code:
public class BookDetail
{
public int Book_Id { get; set; }
public string Book_Title { get; set; }
public int Author_Id { get; set; }
[ForeignKey("Author_Id")]
public AuthorDetail AuthorDetail { get; set; }
}
public class AuthorDetail
{
public int Author_Id { get; set; }
public string Author_FirstName { get; set; }
public string Author_LastName { get; set; }
public virtual ICollection<BookDetail> Books { get; set; }
}
The DbContext for the Book and Author details:
Code:
public class BookDB : DbContext
{
public BookDB()
: base(FiddleHelper.GetConnectionStringSqlServer())
{
}
public DbSet<AuthorDetail> Authors { get; set; }
public DbSet<BookDetail> Books { get; set; }
}
Entity Framework Find Method
The entity Framework Finding method finds the record with the specified primary fundamental values.
Let’s see the following example; it contains the employee details we need to query for the employee id of 1001. The Entity Framework sends the query to the database, but the EF will not send the query to the database; it will use the result already present in the context itself.
Code:
using (dbConContext db = new dbConContext())
{
//initially the query sent to the db and it will be added to the context
var get_Employee1 = db.EmployeeDetails.Find(1);
//now the query will not sent to the database, but it returns entity from the Context itself.
var get_Employee2 = db.EmployeeDetails.Find(1);
}
Benefit of Entity Framework Find
This method returns the record from the context if it is present already; if there is no relevant record present in the context, then the Find method sends the queries to the database.
- Find() is a DbSet method
- Find() executes instantly
Suppose the searching record is already present in the Memory and is being tracked by the context itself. In that case, it removes redundant database queries and returns objects already present in the track.
Code:
using (dbConContext db = new dbConContext())
{
var get_Employee1 = db.EmployeeDetails.Find(1);
var get_Employee2 = db.EmployeeDetails.Find(1);
}
In the above code, it will get the record by tracking. In get_Employee1, initially, the query is sent to the database, and it will be added to the context, and for the get_Employee2, the query will not be sent to the database, but it returns the entity from the context itself.
Conclusion
In this article, we have seen the Entity Framework Find; the specified primary vital values find the records.
Recommended Articles
This is a guide to Entity Framework Find. Here we discuss the introduction and how to use an entity framework find work with method and benefit. You may also have a look at the following articles to learn more –