C# Razor Pages DB Scaffolding at IT College

C# Razor Pages DB Scaffolding at IT College
Slide Note
Embed
Share

Creating C# Razor Pages for DB scaffolding, async operations at IT College with Andres K, 2018-2019 fall semester. Includes defining models, configuring DbContext, adding scaffold tools, and more for efficient development.

  • C#
  • Razor Pages
  • IT College
  • Scaffolding
  • Async

Uploaded on Mar 16, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. 1 C# - Razor Pages Db/Scaffolding/Async IT College, Andres K ver, 2018-2019, Fall semester Web: http://enos.Itcollege.ee/~akaver/csharp Skype: akaver Email: akaver@itcollege.ee

  2. C# - Razor Pages - reminder 2 namespace Domain{ public class Person { public int PersonId { get; set; } Add Domain and DAL in simplest form: [MaxLength(64, ErrorMessage = "Too long!")] [MinLength(2, ErrorMessage = "Too short!")] public string Name { get; set; } } } namespace DAL{ public class AppDbContext: DbContext { public DbSet<Person> Persons { get; set; } public AppDbContext(DbContextOptions options) : base(options){} } }

  3. C# - Razor Pages - reminder 3 Register dbcontext in services (Dependency Injection) public void ConfigureServices(IServiceCollection services) { services.AddDbContext<AppDbContext>(options => options.UseInMemoryDatabase("inmemorydb")); } services.AddMvc;

  4. C# - Razor Pages - reminder 4 public class IndexModel : PageModel { private AppDbContext _db; Access the db in IndexModel public IndexModel(AppDbContext db) { _db = db; } public int ContactCount { get; set; } public List<Person> Persons { get; set; } public void OnGet() { ContactCount = _db.Persons.Count(); Persons = _db.Persons.ToList(); } }

  5. C# - Razor Pages 5 Define all your models Configure DbContext and ModelBuilder Scaffold pages for all models

  6. C# - Razor Pages 6 Install packages into WebApp Microsoft.VisualStudio.Web.CodeGeneration.Tools Microsoft.VisualStudio.Web.CodeGeneration.Design Add scaffold tooling to Web proj file <ItemGroup> <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4"/> </ItemGroup> Run from command line (in solution folder) > dotnet restore Scaffold and migrate your database

  7. C# - Razor Pages 7 Scaffold CRUD pages for your models (cd into WebApp directory first) dotnet aspnet-codegenerator razorpage -m Person -dc AppDbContext -udl -outDir Pages/Persons --referenceScriptLibraries -m Name of the model class -dc -outDir Where to generate the output Data context class -udl Use default layout --referenceScriptLibraries Add validation scripts on Edit and Create pages

  8. C# - Razor Pages 8 Inspect and modify the generated pages to your liking. This is just the starting point!!!!

  9. C# - Razor Pages 9 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!

  10. C# - Razor Pages 10 Method signature public async Task SomeMethodAsync() <- void public async Task<ReturnType> SomeMethodAsync() In method body you can now use await on some method var Person = await _context.People .FirstOrDefaultAsync(m => m.PersonId == id);

  11. C# - Razor Pages 11 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

  12. C# - Razor Pages 12

  13. C# - Razor Pages 13 EF and async Always await DbContext does not support multiple out-of-sync operations. Data corruption can occur.

  14. C# - Razor Pages 14 Other type of IO operations support parallel workloads Web Files Image processing WCF (Xtee)

  15. C# - Razor Pages 15 Running several tasks 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.

More Related Content