在本文中,您将获得以下问题的答案。
- 如何使用查询创建表?
- 如何设置数据库连接并获取操作?
- 需要哪些 NuGet 包?
- 什么是服务?
- 如何在 Program.cs 中注册服务?
- 如何在 Blazor 中实现 CRUD 操作?
- 如何重定向到其他组件?
如何使用查询创建表?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblEmployees](
[EmployeeID] [int] IDENTITY(1,1) NOT NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
) ON [PRIMARY]
GO
首先,在数据库中创建一个名为“tblEmployees”的表。
插入查询
GO
SET IDENTITY_INSERT [dbo].[tblEmployees] ON
INSERT [dbo].[tblEmployees] ([EmployeeID], [FullName], [Address], [City], [State], [Country], [Mobile], [EmailID])
VALUES (1, N'Rajesh Vyas', N'101, Riddhi Apartment, S. V. Road,', N'Mumbai', N'Maharashtra', N'India', N'56464654565', N'rajeshv9292@ygmail.com')
INSERT [dbo].[tblEmployees] ([EmployeeID], [FullName], [Address], [City], [State], [Country], [Mobile], [EmailID])
VALUES (2, N'Kamlesh Purnia', N'202, Prolab Building, Near Mall Cinema, Goregoan-East', N'Mumbai', N'Maharashtra', N'India', N'998787875541', N'kamleshpurnia233@yind.com')
INSERT [dbo].[tblEmployees] ([EmployeeID], [FullName], [Address], [City], [State], [Country], [Mobile], [EmailID])
VALUES (3, N'Ashish Kalla', N'2001, Oberoi Towerr, Near Oberoi Mall, Goregoan-East', N'Mumbai', N'Maharashtra', N'India', N'745564654522', N'ashishkalla@kpmgail.com')
INSERT [dbo].[tblEmployees] ([EmployeeID], [FullName], [Address], [City], [State], [Country], [Mobile], [EmailID])
VALUES (4, N'Suhana Kalla', N'2501, Kankia Tower, Near Tipco, Malad-East,', N'Mumbai', N'Maharashtra', N'India', N'8978787877', N'suhanakalla@podar2.com')
SET IDENTITY_INSERT [dbo].[tblEmployees] OFF
GO
如何设置数据库连接并获取操作?
切换到解决方案资源管理器并在项目文件夹的根目录中选择文件“appsettings.json”,这是应用程序的设置文件。
更新 AllowedHosts 标题下方的连接字符串。
AppSettings.json 文件代码
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Data Source=172.16.0.30;Initial Catalog=dbEmployee;User Id=dbdeveloper;Password=maha@123;TrustServerCertificate=true;MultipleActiveResultSets=true"
}
}
在继续创建实体框架的上下文类之前,请安装以下 NuGet 包。
Microsoft.EntityFrameworkCore
右键单击项目并选择管理 NuGet 包选项。
创建员工模型
右键单击项目,然后单击添加à新项目。
Employee.cs文件代码
using System.ComponentModel.DataAnnotations;
namespace LearnBlazorServerApp
{
public class Employee
{
[Key]
public int EmployeeID { get; set; }
public string FullName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string Mobile { get; set; }
public string EmailID { get; set; }
}
}
创建上下文类
右键单击解决方案资源管理器并选择添加à新项。
添加CLASS文件并指定名称:EmployeeContext.cs。
EmployeeContext.cs 文件代码
using Microsoft.EntityFrameworkCore;
namespace LearnBlazorServerApp
{
public class EmployeeContext : DbContext
{
public EmployeeContext(DbContextOptions<EmployeeContext> options) : base(options)
{
}
public DbSet<Employee> tblEmployees { get; set; } = null!;
}
}
现在我们要更新数据库上下文类服务的 Program.cs 文件。
首先,添加对 EntityFrameworkCore 的引用。
using Microsoft.EntityFrameworkCore;
builder.Services.AddDbContext<EmployeeContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));