Introduction
In this article, we will create a Single Page Application (SPA) using the server-side Blazor concepts with the help of Entity Framework Core database first approach. Single-page applications are web applications that load a single HTML page and dynamically update that page as the user interacts with the app.
We will be creating a sample Employee Record Management System and performing CRUD operations on it. A modal popup will display the form to handle the user inputs and the form also has a dropdown list, which will bind to a database table. We will also provide a filter option to the user to filter the employee records based on the employee name.
We will be using Visual Studio 2017 and SQL Server 2017 for our demo.
Let us look at the final application.

What is a Server-Side Blazor?
The release 0.5.0 of Blazor allows us to run Blazor applications on the server. This means that we can run the Blazor component server-side on .NET Core while other functionalities such as UI updates, event handling, and JavaScript interop calls are handled by a SignalR connection over the network.
For more information, refer to my previous article on Understanding Server-Side Blazor.
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 2012 or above
Visual Studio 2017 versions below v15.7 do not support the Blazor framework.
Source Code
Get the source code for this application from GitHub.
Creating Table
We will be using two tables to store our data.
- Employee: Used to store the details of employees. It contains fields such as EmployeeID, Name, City, Department, and Gender.
- Cities: This contains the list of cities and is used to populate the City field of the Employee table. It contains two fields, CityID and CityName.
Execute the following commands to create both tables.
CREATE TABLE Employee (
EmployeeID int IDENTITY(1,1) PRIMARY KEY,
Name varchar(20) NOT NULL,
City varchar(20) NOT NULL,
Department varchar(20) NOT NULL,
Gender varchar(6) NOT NULL
)
GO
CREATE TABLE Cities (
CityID int IDENTITY(1,1) NOT NULL PRIMARY KEY,
CityName varchar(20) NOT NULL
)
GO
Now, we will put some data into the Cities table. We will be using this table to bind a dropdown list in our web application from which the user will select the desired city. Use the following insert statements.
INSERT INTO Cities VALUES('New Delhi');
INSERT INTO Cities VALUES('Mumbai');
INSERT INTO Cities VALUES('Hyderabad');
INSERT INTO Cities VALUES('Chennai');
INSERT INTO Cities VALUES('Bengaluru');
Now, our Database part is complete. Therefore, we will proceed to create the server-side application using Visual Studio 2017.
Create Server-side Blazor Application
Open Visual Studio and select File >> New >> Project.
After selecting the project, a "New Project" dialog will open. Select .NET Core inside the Visual C# menu from the left panel. Then, select “ASP.NET Core Web Application” from the available project types. Put the name of the project as ServerSideSPA 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.1” from these dropdowns. Then, select the “Blazor (Server-side in ASP.NET Core)” template and press OK.

This will create our server-side Blazor solution. You can observe the folder structure in Solution Explorer, as shown in the below image.

The solution has two project files.
- ServerSideSPA.App: This is our server-side Blazor app. This project has all our component logic and our services. We will also create our models and data access layer in this project.
- ServerSideSPA.Server: This is the ASP.NET Core hosted application. Instead of running client-side in the browser, the server-side Blazor app runs in the ASP.NET Core host application.
In the future release of Blazor, these two projects might be merged into one, but for now, the separation is required due to the differences in the Blazor compilation model.
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 ServerSideSPA.App project.
Navigate to Tools >> NuGet Package Manager >> Package Manager Console. Select “ServerSideSPA.App” from the Default project drop-down. Refer to the 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.
Install-Package Microsoft.EntityFrameworkCore.SqlServer
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.
Install-Package Microsoft.EntityFrameworkCore.Tools
After you have installed both packages, we will scaffold our model from the database tables using the following command.
Scaffold-DbContext "Your connection string here" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables Employee, Cities
Do not forget to put your own connection string (inside " "). After this command gets executed successfully you can observe a Models folder has been created inside ServerSideSPA.App project and it contains three class files myTestDBContext.cs, Cities.cs and Employee.cs. Hence, we have successfully scaffolded our Models using the EF core database first approach.
Creating a data access layer for the application
Right-click on ServerSideSPA.App project and then select Add >> New Folder and name the folder as DataAccess. We will be adding our class to handle database-related operations inside this folder only.
Right-click on the DataAccess folder and select Add >> Class. Name your class EmployeeDataAccessLayer.cs.Open EmployeeDataAccessLayer.cs and put the following code into it.
using Microsoft.EntityFrameworkCore;
using ServerSideSPA.App.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ServerSideSPA.App.DataAccess
{
public class EmployeeDataAccessLayer
{
myTestDBContext db = new myTestDBContext();
// To Get all employees details
public List<Employee> GetAllEmployees()
{
try
{
return db.Employee.AsNoTracking().ToList();
}
catch
{
throw;
}
}
// To Add new employee record
public void AddEmployee(Employee employee)
{
try
{
db.Employee.Add(employee);
db.SaveChanges();
}
catch
{
throw;
}
}
// To Update the records of a particular employee
public void UpdateEmployee(Employee employee)
{
try
{
db.Entry(employee).State = EntityState.Modified;
db.SaveChanges();
}
catch
{
throw;
}
}
// Get the details of a particular employee
public Employee GetEmployeeData(int id)
{
try
{
var employee = db.Employee.Find(id);
db.Entry(employee).State = EntityState.Detached;
return employee;
}
catch
{
throw;
}
}
// To Delete the record of a particular employee
public void DeleteEmployee(int id)
{
try
{
Employee emp = db.Employee.Find(id);
db.Employee.Remove(emp);
db.SaveChanges();
}
catch
{
throw;
}
}
// To get the list of Cities
public List<Cities> GetCityData()
{
try
{
return db.Cities.ToList();
}
catch
{
throw;
}
}
}
}
Here, we have defined the methods to handle database operations. GetAllEmployees will fetch all the employee data from the Employee table. Similarly, AddEmployee 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. GetCityData will fetch the list of all the cities from the Cities table.
Creating the Service class
Right-click on the Services folder and select Add >> Class. Give the name of the class as “EmployeeService.cs” and click Add. This will add the EmployeeService class to the Services folder.
Open EmployeeService.cs and put the following code into it.
using ServerSideSPA.App.DataAccess;
using ServerSideSPA.App.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ServerSideSPA.App.Services
{
public class EmployeeService
{
EmployeeDataAccessLayer objemployee = new EmployeeDataAccessLayer();
public Task<List<Employee>> GetEmployeeList()
{
return Task.FromResult(objemployee.GetAllEmployees());
}
public void Create(Employee employee)
{
objemployee.AddEmployee(employee);
}
public Task<Employee> Details(int id)
{
return Task.FromResult(objemployee.GetEmployeeData(id));
}
public void Edit(Employee employee)
{
objemployee.UpdateEmployee(employee);
}
public void Delete(int id)
{
objemployee.DeleteEmployee(id);
}
public Task<List<Cities>> GetCities()
{
return Task.FromResult(objemployee.GetCityData());
}
}
}
We will invoke the methods of the EmployeeDataAccessLayer class from our service. The service will be injected into our components and the components will call the service methods to access the database.
At this point in time, the ServerSideSPA.The app project has the following structure.

Configuring the Service
To make the service available to the components we need to configure it on the server side app. Open ServerSideSPA.App >> Startup. cs file. Add the following line inside the ConfigureServices method of the Startup class.
services.AddSingleton<EmployeeService>();
Refer to the image below.

Now we will proceed to create our view component.
Creating the View Component
We will add the Razor page in ServerSideSPA.App/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 fetch data and counter pages from ServerSideSPA.App/Pages folder.
Right-click on ServerSideSPA.App/Pages folder and then 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 the templates panel and name it EmployeeData.cshtml. Click Add.

This will add an EmployeeData.cshtml page to the 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
Open EmployeeData.cshtml page and put the following code into it.








Jason QuackenbushPosted Sep 6, 2018, 10:18 AM
How would this be deployed to iis?
Ian WilliamsPosted Aug 22, 2018, 8:11 AM
I've used this example and tried to implement some server side validation. The validation works well and the add/edit form shows any validation errors such as empty fields. There is an issue however with the way the base example works. If you edit a row and change a value eg. @emp.Name, validating employee in SaveEmployee and NOT saving the record, on changing/deleting another row then the first row will also be updated in the database.It appears that updating @emp.Name etc in the form will update the original selected object in the Db context. The server side is not de-coupled from the form. (If that makes sense?)
David MastersonPosted Aug 14, 2018, 2:10 AM
Thank you for this post. I have been looking for a clearly defined example of EF in blazor
David MastersonPosted Aug 14, 2018, 2:09 AM
Also the blazor team has recognised the need for a built in forms validation system they just have not built it yet.
Ian WilliamsPosted Aug 13, 2018, 7:54 AM
How would you implement form validation with server side blazor?