Introduction
In this article, we are going to create a Single Page Application (SPA) using Razor pages in Blazor with the help of Entity Framework Core database first approach. Single-Page Applications are web application that load a single HTML page and dynamically update that page as the user interacts with the app. We will create a sample Employee Record Management System and perform CRUD operations on it.
We will use Visual Studio 2017 and SQL Server 2014.
Take a look at the final application.

Prerequisites
- Install the .NET Core 2.1 or above SDK from here
- Install Visual Studio 2017 v15.7 or above from here
- Install ASP.NET Core Blazor Language Services extension from here
- SQL Server 2008 or above
Blazor framework is not supported by versions below Visual Studio 2017 v15.7.
Source Code
The source code has been updated to .NET Core 3.2 Preview-1. Get the source code from Github.
Creating Table
We will use a DB table to store all the records of employees.
Open SQL Server and use the following script to create Employee table.
Create Blazor Web Application
Open Visual Studio and select File >> New >> Project.
After selecting the project, a “New Project” dialog will open. Select .NET Core inside Visual C# menu from the left panel. Then, select “ASP.NET Core Web Application” from available project types. Put the name of the project as
BlazorSPA and press OK.
After clicking on OK, a new dialog will open asking you to select the project template. You can observe two drop-down menus at the top left of the template window. Select “.NET Core” and “ASP.NET Core 2.0” from these dropdowns. Then, select “Blazor (ASP .NET Core hosted)” template and press OK.

It will create the Blazor solution. You can observe the folder structure in Solution Explorer as shown in the image below.

You can observe that we have three project files created inside this solution.
- BlazorSPA.Client – It has the client side code and contains the pages that will be rendered on the browser.
- BlazorSPA.Server – It has the server side codes such as DB related operations and web API.
- BlazorSPA.Shared – It contains the shared code that can be accessed by both client and server. It contains out Model classes.
Scaffolding the Model to the Application
We are using Entity Framework core database first approach to create our models. We will create our model class in
BlazorSPA.Shared project so that it can be accessible to both client and server project.
Navigate to Tools >> NuGet Package Manager >> Package Manager Console. Select “BlazorSPA.Shared” from Default project drop-down. Refer to image below:

First, we will install the package for the database provider that we are targeting which is SQL Server in this case. Hence, run the following command,
Since we are using Entity Framework Tools to create a model from the existing database, we will install the tools package as well. Hence, run the following command
After you have installed both the packages, we will scaffold our model from the database tables using the following command,
Do not forget to put your own connection string (inside ” “). The successful execution of this command will create a folder called
Models having two class files myTestDBContext.cs and Employee.cs. The name of your DBContext class will be the name of your database suffixed with the word Context. Here my database name is myTestDB, hence the context class name is myTestDBContext. Hence, we have successfully scaffolded our Models using EF core database first approach.
At this point in time, the Models folder has the following structure.

Creating Data Access Layer for the Application
Right-click on
BlazorSPA.Server project and then select Add >> New Folder. Name the folder as DataAccess. We will be adding our class to handle database related operations inside this folder only.
Right click on DataAccess folder and select Add >> Class. Name your class
EmployeeDataAccessLayer.cs.
Open
EmployeeDataAccessLayer.cs and put the following code into it.
Here we have defined methods to handle database operations.
GetAllEmployees will fetch all the employee data from Employee Table. The AddEmployee method will create a new employee record and UpdateEmployee will update the record of an existing employee. GetEmployeeData will fetch the record of the employee corresponding to the employee ID passed to it and DeleteEmployee will delete the employee record corresponding to the employee id passed to it.Adding the web API Controller to the Application
Right click on
BlazorSPA.Server/Controllers folder and select Add >> New Item. An “Add New Item” dialog box will open. Select ASP.NET from the left panel, then select “API Controller Class” from templates panel and put the name as EmployeeController.cs. Click Add.
This will create our API EmployeeController class.
We will call the methods of
EmployeeDataAccessLayer class to fetch data and pass on the data to the client side.
Open
EmployeeController.cs file and put the following code into it.
At this point of time, our
BlazorSPA.Server project has the following structure.
We are done with our backend logic. Therefore, we will now proceed to code our client side.
Adding Razor Page to the Application
We will add the Razor page in
BlazorSPA.Client/Pages folder. By default, we have “Counter” and “Fetch Data” pages provided in our application. These default pages will not affect our application but for the sake of this tutorial, we will delete fetchdata and counter pages from the BlazorSPA.Client/Pages folder.
Right click on
BlazorSPA.Client/Pages folder and select Add >> New Item. An “Add New Item” dialog box will open, select “ASP.NET Core” from the left panel, then select “Razor Page” from templates panel and name it EmployeeData.cshtml. Click Add.
This will add an
EmployeeData.cshtml page to our BlazorSPA.Client/Pages folder. This razor page will have two files, EmployeeData.cshtml and EmployeeData.cshtml.cs.
Now we will add codes to these pages.
EmployeeData.cshtml.cs
Open
EmployeeData.cshtml.cs and put the following code into it
Let us understand this code. We have defined a class EmployeeDataModel that will hold all our methods that we will use in EmployeeData.cshtml page.
We are injecting the
HttpClient service to enable web API call and IUriHelper service to enable URL redirection. After this, we have defined our parameter attributes – paramEmpID and action. These parameters are used in EmployeeData.cshtml to define the routes for our page. We have also declared a property title to display the heading to specify the current action that is being performed on the page.
The
OnParametersSetAsync method is invoked whenever the URL parameters are set for the page. We will check the value of parameter “action” to identify the current operation on the page. If the action is set to “fetch”, then we will invoke FetchEmployee method to fetch the updated list of employees from the database and refresh the UI using StateHasChanged method. We will check if the action attribute of parameter is set to “create”, then we will set the title of page to “Add Employee” and create a new object of type Employee. If the paramEmpID is not “0”, then it is either an edit action or a delete action. We will set the title property accordingly and then invoke our web API method to fetch the data for the employee id as set in paramEmpID property.
The method
FetchEmployee will set the title to “Employee Data” and fetch all the employee data by invoking our web API method.
The
CreateEmployee method will check if it is invoked to add a new employee record or to edit an existing employee record. If the EmployeeId property is set then it is an “edit” request and we will send a PUT request to web API. If EmployeeId is not set then it is a “create” request and we will send a POST request to web API. We will set the title property according to the corresponding value of action and then invoke our web API method to fetch the data for the employee id as set in paramEmpID property.
The
DeleteEmployee method will delete the employee record for the employee id as set in paramEmpID property. After deletion, we will redirect the user to /employee/fetch page.
In the Cancel method we will set the title property to “Employee Data” and redirect the user to
/employee/fetch page.EmployeeData.cshtml
Open
EmployeeData.cshtml page and put the following code into it.
At the top, we have defined the routes for our page. There are two routes defined.
- /employee/{action}/{paramEmpID} : This will accept action name along with employee id. This route is invoked when we perform Edit or Delete operation. When we call edit or delete action on a particular employee data, the employee id is passed as the URL parameter.
- /employee/{action} : This will only accept the action name. This route is invoked when we create a new employee data or we fetch the records of all the employees.
We are also inheriting
EmployeeDataModel class, which is defined in EmployeeData.cshtml.cs file. This will allow us to use the methods defined in EmployeeDataModel class.
After this, we are setting the title that will be displayed on our page. The title is dynamic and change as per the action that is being executed currently on the page.
We will show the “Create New” link only if the action is “fetch”. If the action is create or edit then “Create New” link will be hidden and we will display the form to get the user input. Inside the form, we have also defined two buttons “Save” and “Cancel”. Clicking on the Save button will invoke the
CreateEmployee method. Clicking on the Cancel will invoke the Cancel method.
If the action is delete then a table will be displayed with the data of the employee on which the delete action is invoked. We will also display two buttons – “Delete” and “Cancel”. Clicking on the Delete button will invoke the
DeleteEmployee method. Clicking on the Cancel will invoke the Cancel method.
At the end, we have a table to display all the employee data from the database. Each employee record will also have two action links, Edit to edit the employee record and Delete to delete the employee record. This table is always displayed on the page and we will update it after performing every action.
Adding Link to Navigation menu
The last step is to add the link to our “EmployeeData” page in the navigation menu, open
BlazorSPA.Client/Shared/NavMenu.cshtml page and put the following code into it.
Hence, we have successfully created a Single Page Application (SPA) using Blazor with the help of Entity Framework Core database first approach.
Execution Demo
Press F5 to launch the application.
A web page will open as shown in the image below. The navigation menu on the left is showing navigation link for Employee data page.

Click on “Employee data” link, it will redirect to EmployeeData view. Here you can see all the employee data on the page. Notice the URL has “employee/fetch” appended to it.

Since we have not added any data, hence it is empty. Click on CreateNew to open “Add Employee” form to add a new employee data. Notice the URL has “employee/create” appended to it

After inserting data in all the fields, click on “Save” button. The new employee record will be created and the Employee data table will get refreshed.

If we want to edit an existing employee record, we will click on Edit action link. It will open Edit view as shown below. Here we can change the employee data. Notice that we have passed the employee id in the URL parameter.

Here we have changed the City of employee Swati from Mumbai to Kolkatta. Click on “Save” to refresh the employee data table to view the updated changes as highlighted in the image below:

Now, we will perform Delete operation on the employee named Dhiraj. Click on Delete action link which will open Delete view asking for a confirmation to delete. Notice that we have passed the employee id in the URL parameter.

Once we click on the Delete button, it will delete the employee record and the employee data table will be refreshed. Here, we can see that the employee with name Dhiraj has been removed from our record.

Deploying the application
To learn how to deploy a Blazor application using IIS, refer to Deploying A Blazor Application On IIS
To deploy this application on Azure, refer to Deploying A Blazor Application On Azure
Conclusion
We have created a Single Page Application with Razor pages in Blazor using the Entity Framework Core database first approach with the help of Visual Studio 2017 and SQL Server 2014. We also performed CRUD operations on our application.
Please get the source code from Github and play around to get a better understanding.


0 comments:
Post a Comment
Note: only a member of this blog may post a comment.