Blazor is a free and open-source web framework that enables developers to create web apps using C# and HTML. Blazor is a framework for building interactive client-side web UI with .NET.
Create rich interactive UIs using C# instead of JavaScript.
Share server-side and client-side app logic written in .NET.
Render the UI as HTML and CSS for wide browser support, including mobile browsers.
Integrate with modern hosting platforms, such as Docker.
Create Blazor App
To start, we will create an ASP.NET Core hosted Blazor App project. The project type comes with all template files to create a Blazor application before adding anything. Let's open Visual Studio 2019. If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.
On the start window, choose Create a new project.
On the Create a new project window, enter or type blazor in the search box. Next, choose C# from the Language list. Select the Blazor App template, and then choose Next.
In the Configure your new project window, type or enter EFCore5InBlazorApp in the Project name box and click on the Create button.
Now select the Blazor WebAssembly App project template and select the ASP.NET Core hosted check box. Click on the Create button to create an ASP.NET Core-hosted application.
Visual Studio opens your new project and includes the default code files in your project as shown in the Solution Explorer. You will see that 3 projects are created inside this solution.
EFCore5InBlazorApp.Client: It has the client-side code and contains the pages that will be rendered on the browser.
EFCore5InBlazorApp.Server: It has the server-side code, such as DB related operations and the web API.
EFCore5InBlazorApp.Shared: It contains the shared code that can be accessed by both client and server.
Install Entity Framework Core
To use Entity Framework Core we need to install Microsoft.EntityFrameworkCore library in the EFCore5InBlazorApp.Server project. It is available as a nuget package, and you can install it using Nuget Package Manager.
In the Package Manager Console window, enter the following command.
PM> Install-PackageMicrosoft.EntityFrameworkCore
For SQL Server LocalDB, which is installed with Visual Studio, we need to install Microsoft.EntityFrameworkCore.SqlServer and will get all the packages required for EF Core.
To add a data model to the application, right-click on EFCore5InBlazorApp.Shared project and then select Add > New Folder and name the folder Models. Right-click on the Models folder and select Add > Class..., enter a class file name Author.cs, and add the following code.
So let's create a folder in your project by right-clicking on EFCore5InBlazorApp.Server project in Solution Explorer and click Add > New Folder. Name the folder DAL (Data Access Layer). In that folder, create a new class file named AuthorContext.cs, and replace the following code.
To register AuthoContext as a service, open Startup.cs from EFCore5InBlazorApp.Server project, and call the AddDnContext in the ConfigureServices method.
// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollection services){ services.AddDbContext<BookStore>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddControllersWithViews();}
The name of the connection string is passed into the context by calling a method on a DbContextOptionsBuilder object.
Setup Connection String
For local development, the ASP.NET Core configuration system reads the connection string from the appsettings.json file. So let's add the connection to that file as shown below.
The above connection string specifies that the Entity Framework will use a LocalDB database named AuthorDb.
Initialize Database
The Entity Framework will create an empty database for you. So we need to write a method that's called after the database is created to populate it with test data.
In the DAL folder of EFCore5InBlazorApp.Server project, add a new class AuthorContextInitializer and replace the following code.
usingEFCore5InBlazorApp.Shared.Models;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;namespaceEFCore5InBlazorApp.Server.DAL{publicclassAuthorContextInitializer {publicstaticvoidInitialize(AuthorContext context) {context.Database.EnsureCreated(); // Look for any authors.if (context.Authors.Any()) {return; // DB has been seeded }var authors =newList<Author> {newAuthor { Name="Carson Alexander", Gender="Male", Address ="Skagen 21, Stavanger, Norway"},newAuthor { Name="Meredith Alonso", Gender="Female", Address ="Keskuskatu 45, Helsinki, Finland"}, new Author { Name="Arturo Anand", Gender="Male", Address = "305 - 14th Ave. S. Suite 3B, Seattle, USA"},
};authors.ForEach(a =>context.Authors.Add(a));context.SaveChanges(); } }}
The above code creates a database when needed and loads test data into the new database.
It also checks if there are any authors in the database, and if not, it assumes the database is new and needs to be seeded with test data.
In Program.cs file of EFCore5InBlazorApp.Server project, replace the following code in the Main method.
To create a controller, right-click the Controllers folder in EFCore5InBlazorApp.Server, and select Add > Controller... and it will open the Add Scaffold dialog box.
Select API Controller - Empty, and then click the Add button.
Select API Controller - Empty, enter AuthorController (not AuthorsController) as a Controller name and click the Add button. It will create an empty controller, let's add the following code which contains all the basic CRUD operations.
To display all the authors, we need to create a view by right-clicking on the Pages folder in EFCore5InBlazorApp.Client project and select Add > Razor Component...
Select Razor Component, enter GetAuthors.razor as a Name, and click the Add button. It will create an empty view and then add the following code.
@page "/getauthors"@using EFCore5InBlazorApp.Shared.Models@inject HttpClient Http<h1>Authors</h1><p>This component demonstrates fetching Author data from the server.</p><p><a href="/addauthor">Create New</a></p>@if (authors ==null){<p><em>Loading...</em></p>}else{<table class='table'><thead><tr><th>Name</th><th>Gender</th><th>Address</th></tr></thead><tbody>@foreach (var author in authors) {<tr><td>@author.Name</td><td>@author.Gender</td><td>@author.Address</td><td><a href='/editemployee/@author.Id'>Edit</a>|<a href='/delete/@author.Id'>Delete</a></td></tr> }</tbody></table>}@code {Author[] authors; protected override async Task OnInitializedAsync() { authors =awaitHttp.GetFromJsonAsync<Author[]>("/api/Author/Index"); }}
Now let's add one more page that will use to add a new author, call it AddAuthor.razor, and add the following code.