Find Method in LINQ MeThods in Entity Framework Core

Find method is used to find an entity with the given primary key values. If found, is attached to the context and returned. If no entity is found, then null is returned.

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var product = databaseContext.Products.Find(9);
                if (product != null)
                {
                    Console.WriteLine("Id: " + product.Id);
                    Console.WriteLine("Name: " + product.Name);
                    Console.WriteLine("Price: " + product.Price);
                    Console.WriteLine("Quantity: " + product.Quantity);
                    Console.WriteLine("Description: " + product.Description);
                    Console.WriteLine("Status: " + product.Status);
                    Console.WriteLine("Photo: " + product.Photo);
                    Console.WriteLine("Created: " + product.Created.ToString("dd/MM/yyyy"));
                    Console.WriteLine("Category Id: " + product.CategoryId);
                }
                else
                {
                    Console.WriteLine("Not Found");
                }
            }
        }
    }
}
Id: 9
Name: Tivi 1
Price: 10
Quantity: 15
Description: Description 1
Status: True
Photo: photo1.gif
Created: 20/10/2023
Category Id: 1

Finding an entity by Composite Primary Key:

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var invoiceDetail = databaseContext.InvoiceDetails.Find(1, 9);
                if (invoiceDetail != null)
                {
                    Console.WriteLine("Invoice Id: " + invoiceDetail.InvoiceId);
                    Console.WriteLine("Product Id: " + invoiceDetail.ProductId);
                    Console.WriteLine("Price: " + invoiceDetail.Price);
                    Console.WriteLine("Quantity: " + invoiceDetail.Quantity);
                }
                else
                {
                    Console.WriteLine("Not Found");
                }
            }
        }
    }
}
Invoice Id: 1
Product Id: 9
Price: 4.5
Quantity: 20