.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();
        }
    }
}
相关推荐
做一个AK梦8 分钟前
论基于云原生数据库的企业信息系统架构设计
数据库·云原生
风哥2号18 分钟前
PostgreSQL数据库恢复工具FGPDU(FGEDU PostgreSQL DUL)
数据库·postgresql
智购科技智能售货柜22 分钟前
2026自动售货机峰值交易流量应对方案:从消息削峰到弹性扩容的工程实践~YH
数据库·人工智能·单片机·嵌入式硬件·yolo
JavaPub-rodert33 分钟前
Ontop 详解:不搬数据库,也能把 MySQL / PostgreSQL 变成知识图谱
数据库·mysql·postgresql
xiaoxiang96091 小时前
Git 分支同步实战:`merge -X theirs` 与完全合并方案
数据库·git·elasticsearch
GoppViper1 小时前
用户测试如何提升SEO转化率?四种实操方法与案例解析
数据库·经验分享
先吃饱再说2 小时前
后端开发绕不开的 SQL:从建表、查询到索引优化,一篇讲透
数据库·后端·sql
敲代码的小小酥2 小时前
InfluxDB时序数据库(续)
数据库·时序数据库
智购科技自动贩卖机2 小时前
自动售货机嵌入式系统Go语言开发实践:从资源受限设备到RTOS协程调度的工程化之路
大数据·linux·数据库·人工智能·yolo·架构·golang
SelectDB2 小时前
2026 年 Apache Doris 和 StarRocks 怎么选?一篇更接近真实选型的对比
大数据·数据库·数据分析