Book Image

Web Development with Blazor

By : Jimmy Engström
Book Image

Web Development with Blazor

By: Jimmy Engström

Overview of this book

Blazor is an essential tool if you want to build interactive web apps without JS, but it comes with its own learning curve. Web Development with Blazor will help you overcome most common challenges developers face when getting started with Blazor and teach you the best coding practices. You’ll start by learning how to leverage the power of Blazor and explore the full capabilities of both Blazor Server and Blazor WebAssembly. Then you’ll move on to the practical part, which is centred around a sample project – a blog engine. This is where you’ll apply all your newfound knowledge about creating Blazor Server and Blazor WebAssembly projects, the inner working of Razor syntax, and validating forms, as well as creating your own components. You’ll learn all the key concepts involved in web development with Blazor, which you’ll also be able to put into practice straight away. By showing you how all the components work together practically, this book will help you avoid some of the common roadblocks that novice Blazor developers face and inspire you to start experimenting with Blazor on your other projects. When you reach the end of this Blazor book, you'll have gained the confidence you need to create and deploy production-ready Blazor applications.
Table of Contents (20 chapters)
1
Section 1:The Basics
4
Section 2:Building an Application with Blazor
14
Section 3:Debug, Test, and Deploy

Adding the DbContext to Blazor

Using DbContext, we will be able to access the data from our database. We need to add DbContext to our Blazor project to be able to access the data from Blazor:

  1. Beneath the MyBlogServerSide node in Solution Explorer, find Dependencies. Right-click on Dependencies and select Add Project reference.
  2. In the list of projects, check the MyBlog.Data project and click OK:

    Figure 3.2 – Visual Studio Reference manager

    Now we have all the external items in place, including the external NuGet packages and a reference to our MyBlog.Data project.

  3. In the MyBlogServerSide project, open the Startup.cs file and add the following using statements:
    using Microsoft.EntityFrameworkCore;
    using MyBlog.Data;
    using MyBlog.Data.Interfaces;
    using MyBlogServerSide.Data;
  4. Add following code to the ConfigureServices method:
    services.AddDbContextFactory<MyBlogDbContext>(opt => opt.UseSqlite($"Data Source=../MyBlog.db"));
    services.AddScoped&lt...