【3】Blazor链接数据
一、引入Nuget包
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
二、添加链接字符串
在文件appsetting.Json中添加ConnectionStrings配置信息
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;"
},
"AllowedHosts": "*"
}
三、创建DbContext
添加UserInfo的Model类
public class UseInfo
{
public int Id {
get; set; }
public string Name {
get; set; }
public int Age {
get; set; }
}
添加TrendSimulationDbContext 数据库上下文类
public class TrendSimulationDbContext : DbContext
{
public TrendSimulationDbContext(DbContextOptions<TrendSimulationDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
public DbSet<UseInfo> UserInfos {
get; set; }
}
四、注入SqlServer数据库
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft