.net8 blazor auto模式很爽(五)读取sqlite并显示(1)

为了访问sqlite,我们需要在Nuget中安装Microsoftr的EntityFrameworkCore、EntityFrameworkCore.Sqlite、EntityFrameworkCore.Sqlite.Core

在SharedLibrary的Models里增加employee

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharedLibrary.Models
{
    
        public class employee
        {
            public string? 人员姓名 { get; set; }
            public string? 证件号码 { get; set; }
            public string? 单位名称 { get; set; }
            public string? 性别 { get; set; }
        }
    
}

我们在Client的Pages里面增加一个Testtable.razor,里面放一个table用来显示sqlite里面一张表的内容。

cs 复制代码
@page "/testtable"
@rendermode InteractiveAuto

<PageTitle>测试sqlite取数</PageTitle>

    <button class="btn btn-primary" @onclick="getemployees">获取数据</button>
    <table class="table">
        <thead>
            <tr>
                <th>人员姓名</th>
                <th>证件号码</th>
                <th>单位名称</th>
                <th>性别</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var forecast in employees)
            {
                <tr>
                    <td>@forecast.人员姓名</td>
                    <td>@forecast.证件号码</td>
                    <td>@forecast.单位名称</td>
                    <td>@forecast.性别</td>
                </tr>
            }
        </tbody>
    </table>


@code {
    private List<employee> employees=new List<employee>();
    
    protected override async Task   OnInitializedAsync()
    {
    }
    private async Task getemployees()
    {
        employees = await EmployeeService.GetEmployeesAsync();
        StateHasChanged(); // 通知 Blazor 组件数据已更新
    }
    


}

在BlazorApp1的Controllers里增加EmployeeController:

cs 复制代码
using Microsoft.AspNetCore.Mvc;
using SharedLibrary.Models;
using BlazorApp1.data;
using Microsoft.EntityFrameworkCore;

namespace BlazorApp1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController
    {
        employee employeels = new employee();
        private readonly dbcont _context;

        public EmployeeController(dbcont context)
        {
            _context = context;
        }
        [HttpGet("Getemployee")]
        public async Task<ActionResult<IEnumerable<employee>>> Getemployee()
        {
            return await _context.employee.Take(10).ToListAsync();
        }
    }
}
相关推荐
薛定谔的算法14 小时前
phoneGPT:构建专业领域的检索增强型智能问答系统
前端·数据库·后端
Databend15 小时前
Databend 亮相 RustChinaConf 2025,分享基于 Rust 构建商业化数仓平台的探索
数据库
得物技术16 小时前
破解gh-ost变更导致MySQL表膨胀之谜|得物技术
数据库·后端·mysql
Raymond运维21 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
沢田纲吉21 小时前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
RestCloud2 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud2 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence2 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger2 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql