Introduction


What is Server-Side Blazor?
Prerequisites
Source Code
Important Note:
Creating a Table
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
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');
Create The Server Side Blazor Application



The solution has two project files:
Scaffolding the Model to the Application

Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Scaffold-DbContext "Your connection string here" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables Employee, Cities
Creating the Data Access Layer for the Application
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 particluar 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;
}
}
}
}
Creating the Service class

Configuring the Service
services.AddSingleton<EmployeeService>();

Creating The View Component

EmployeeData.cshtml
EmployeeData.cshtml.cs
Execution Demo







Conclusion