Skip to content

08 - Razor Pages - Scaffolding


  • Define all your domain models
  • Configure DbContext and ModelBuilder
  • Scaffold (generate) crud pages for all domain models


  • Install EF tools
    > dotnet tool install --global dotnet-ef
  • Install needed nuget packages
    Microsoft.EntityFrameworkCore.SQLite Microsoft.VisualStudio.Web.CodeGeneration.Design Microsoft.EntityFrameworkCore.Design Microsoft.EntityFrameworkCore.SqlServer
  • Scaffold and migrate your database
    dotnet ef migrations add InitialCreate dotnet ef database update
  • Install UI scaffolding tool
    > dotnet tool install --global dotnet-aspnet-codegenerator


Scaffold CRUD pages for your models:


cd into WebApp directory first!

dotnet aspnet-codegenerator razorpage 
    -m Person 
    -dc AppDbContext 
    -outDir Pages/Persons 
  • -m - Name of the model class
  • -dc - Data context class
  • -udl - Use default layout
  • -outDir - Where to generate the output
  • --referenceScriptLibraries - Add validation scripts on Edit and Create pages

After scaffolding


Inspect and modify the generated pages to your liking. This is just the starting point!


  • New concept Async/Wait
  • Two ways of looking at parallel work – CPU based vs IO based

    • CPU based – hard to implement correctly. Even harder to design correctly
    • IO based – framework helps. Async pattern in .net
  • OS Thread generation is expensive and slow!

Method signature and usage

Method signature:

  • public async Task SomeMethodAsync() <- void
  • public async Task<ReturnType> SomeMethodAsync()

In method body you can now use await on some async method:

var Person = await _context.People.FirstOrDefaultAsync(m => m.PersonId == id);

During runtime

During runtime, when code hits await

  • Async call starts to execute
  • State machine is created and control is given back to server
  • When your data arrives, control returns to the method

Async flow


Entity Framework and Async

  • Always await
  • DbContext does not support multiple out-of-sync operations. Data corruption can occur.

TODO: Title of the header

Other type of IO operations support parallel workloads:

  • Web
  • Files
  • Image processing
  • WCF (XRoad)

Running several task in parallel

  • Create and collect many tasks
    List<Task<SomeType>> allMyTasks
  • Use WhenAll
    List<SomeType> results = await Task.WhenAll(allMyTasks)
  • Write wrapper methods if tasks are different