liveBook · Manning

FindAsync

As explained in ASP.NET Core in Action, 3e

FindAsync is an Entity Framework Core DbSet method that asynchronously loads an entity by its primary key, first checking tracked entities.

FindAsync asynchronously loads an entity from a DbSet by its primary key. Before querying the database, it checks whether the entity is already tracked by the current DbContext and returns the tracked instance when available. section 12.4.4

Load a recipe before updating it

var recipe = await _context.Recipes.FindAsync(cmd.Id); if (recipe is null) { throw new Exception("Unable to find the recipe"); } UpdateRecipe(recipe, cmd); await _context.SaveChangesAsync(); This keeps the read, modification, and save steps explicit. FindAsync can avoid a database call when the recipe is already tracked; use a LINQ query instead when you know it is not tracked and that distinction matters. section 12.4.4

Listing 12.9 Updating an existing entity with EF Core in RecipeService
public async Task UpdateRecipe(UpdateRecipeCommand cmd)
{
    var recipe = await _context.Recipes.FindAsync(cmd.Id);         #1
    if(recipe is null) {                                           #2
        throw new Exception("Unable to find the recipe");          #2
    }                                                              #2
    UpdateRecipe(recipe, cmd);                                     #3
    await _context.SaveChangesAsync();                             #4
}
 
static void UpdateRecipe(Recipe recipe, UpdateRecipeCommand cmd)   #5
{                                                                  #5
    recipe.Name = cmd.Name;                                        #5
    recipe.TimeToCook =                                            #5
        new TimeSpan(cmd.TimeToCookHrs, cmd.TimeToCookMins, 0);    #5
    recipe.Method = cmd.Method;                                    #5
    recipe.IsVegetarian = cmd.IsVegetarian;                        #5
    recipe.IsVegan = cmd.IsVegan;                                  #5
}                                                                  #5

Shows FindAsync in a complete update method, including missing-entity handling and SaveChangesAsync. section 12.4.4

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains lookup, tracked-entity reuse, a LINQ alternative, missing results, and the update-save workflow.
Examples
Uses RecipeService.UpdateRecipe to find, modify, validate, and save a Recipe entity.
What it assumes
Assumes familiarity with DbSet, DbContext, entities, primary keys, and SaveChangesAsync.
What you can do after
Apply via Entity Framework Core by loading an entity, handling a missing result, changing properties, and saving asynchronously.
How it connects
Connects DbSet, DbContext, LINQ, change tracking, and SaveChangesAsync.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.