.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();
        }
    }
}
相关推荐
经典199233 分钟前
mysql 锁介绍
数据库·mysql
不太可爱的大白34 分钟前
Mysql分片:一致性哈希算法
数据库·mysql·算法·哈希算法
~ 小团子35 分钟前
每日一SQL 【游戏玩法分析 IV】
数据库·sql·游戏
零叹39 分钟前
MySQL——常用程序and主从复制
数据库·mysql
胚芽鞘6815 小时前
关于java项目中maven的理解
java·数据库·maven
sun0077008 小时前
mysql索引底层原理
数据库·mysql
workflower11 小时前
MDSE和敏捷开发相互矛盾之处:方法论本质的冲突
数据库·软件工程·敏捷流程·极限编程
Tony小周11 小时前
实现一个点击输入框可以弹出的数字软键盘控件 qt 5.12
开发语言·数据库·qt
noravinsc11 小时前
django 一个表中包括id和parentid,如何通过parentid找到全部父爷id
python·django·sqlite
lifallen11 小时前
Paimon 原子提交实现
java·大数据·数据结构·数据库·后端·算法