当前位置: 首页 > news >正文

Blazor入门100天 : 身份验证和授权 (6) - 使用 FreeSql orm 管理ids数据

目录

  1. 建立默认带身份验证 Blazor 程序
  2. 角色/组件/特性/过程逻辑
  3. DB 改 Sqlite
  4. 将自定义字段添加到用户表
  5. 脚手架拉取IDS文件,本地化资源
  6. freesql 生成实体类,freesql 管理ids数据表
  7. 初始化 Roles,freesql 外键 => 导航属性
  8. 完善 freesql 和 bb 特性

本节源码

https://github.com/densen2014/Blazor100/tree/Blazor-%E6%95%99%E7%A8%8B15-6/b16blazorIDS2

截图

安装 FreeSql.Generator 命令行代码生成器生成实体类

对于此工具的使用可参考 https://github.com/dotnetcore/FreeSql/wiki/DbFirst , 也可直接运行命令查看 FreeSql.Generator

安装 dotnet-tool 生成实体类

dotnet tool install -g FreeSql.Generator

生成实体

  1. 项目右键添加 Model 目录
  2. 右键选择在终端中打开
  3. 输入命令
FreeSql.Generator  -NameOptions 0,0,0,0 -NameSpace b16blazorIDS2.Models.ids -DB "Sqlite,Data Source=../ids.db;" -Filter "View+StoreProcedure" -FileName "{name}.cs"

解释

  • -NameOptions 0,0,0,0 首字母大写, 首字母大写,其他小写, 全部小写, 下划线转驼峰
  • -DB “Sqlite,Data Source=…/ids.db;” 数据库类型和连接字符串,本例数据库在上一级目录,所以是…/ids.db
  • -Filter “View+StoreProcedure” 不生成视图和存储过程

生成的实体

添加 BootstrapBlazor 组件库

相关步骤往日文章有写,不再赘述,只贴上关键部分.

引用Nuget包

        <PackageReference Include="BootstrapBlazor" Version="7.*" /><PackageReference Include="BootstrapBlazor.FontAwesome" Version="7.*" /><PackageReference Include="Densen.Extensions.BootstrapBlazor" Version="7.*" /><PackageReference Include="Densen.FreeSql.Extensions.BootstrapBlazor" Version="7.*" /><PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.685" /><PackageReference Include="Magicodes.IE.Core" Version="2.7.1" /><PackageReference Include="Magicodes.IE.Excel" Version="2.7.1" /><PackageReference Include="Magicodes.IE.Html" Version="2.7.1" /><PackageReference Include="Magicodes.IE.Pdf" Version="2.7.1" /><PackageReference Include="Magicodes.IE.Word" Version="2.7.1" /><PackageReference Include="HtmlToOpenXml.dll" Version="2.3.0" /><PackageReference Include="Haukcode.WkHtmlToPdfDotNet" Version="1.5.86" />

App.razor

<BootstrapBlazorRoot><CascadingAuthenticationState>...</CascadingAuthenticationState>
</BootstrapBlazorRoot>

_Imports.razor

添加的代码

@using BootstrapBlazor.Components
@using AME.Services
@using Blazor100.Service
@using System.Diagnostics.CodeAnalysis

Pages_Host.cshtml

    <!-- 删掉这行 <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" /> !--><link href="css/site.css" rel="stylesheet" /><!-- 添加下面两行 !--><link href="_content/BootstrapBlazor.FontAwesome/css/font-awesome.min.css" rel="stylesheet"><link href="_content/BootstrapBlazor/css/bootstrap.blazor.bundle.min.css" rel="stylesheet"><!-- 添加上面两行 !-->...<!-- <script src="_framework/blazor.server.js"></script> 之前增加这行 !--><script src="_content/BootstrapBlazor/js/bootstrap.blazor.bundle.min.js"></script>

添加导入导出服务

新建 Service 文件夹, 新建 ImportExportsService.cs 文件

using BootstrapBlazor.Components;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using Magicodes.ExporterAndImporter.Html;
using Magicodes.ExporterAndImporter.Pdf;
using Magicodes.ExporterAndImporter.Word;namespace Blazor100.Service
{/// <summary>/// 通用导入导出服务类/// </summary>public class ImportExportsService{public enum ExportType{Excel,Pdf,Word,Html}public async Task<string> Export<T>(string filePath, List<T>? items = null, ExportType exportType = ExportType.Excel) where T : class, new(){switch (exportType){case ExportType.Pdf:var exporterPdf = new PdfExporter();items = items ?? new List<T>();var resultPdf = await exporterPdf.ExportListByTemplate(filePath + ".pdf", items);return resultPdf.FileName;case ExportType.Word:var exporterWord = new WordExporter();items = items ?? new List<T>();var resultWord = await exporterWord.ExportListByTemplate(filePath + ".docx", items);return resultWord.FileName;case ExportType.Html:var exporterHtml = new HtmlExporter();items = items ?? new List<T>();var resultHtml = await exporterHtml.ExportListByTemplate(filePath + ".html", items);return resultHtml.FileName;default:IExporter exporter = new ExcelExporter();items = items ?? new List<T>();var result = await exporter.Export(filePath + ".xlsx", items);return result.FileName;}}public async Task<(IEnumerable<T>? items,string error)> ImportFormExcel<T>(string filePath) where T : class, new(){IExcelImporter Importer = new ExcelImporter();var import = await Importer.Import<T>(filePath);if (import.Data == null ) {return (null, import.Exception.Message);}return (import.Data!.ToList(),""); }}
}

Program.cs

顶上添加

using Densen.DataAcces.FreeSql;
using Blazor100.Service;

builder.Services.AddSingleton<WeatherForecastService>(); 下面添加

builder.Services.AddFreeSql(option =>
{option.UseConnectionString(FreeSql.DataType.Sqlite, "Data Source=ids.db;")  //也可以写到配置文件中
#if DEBUG//开发环境:自动同步实体.UseAutoSyncStructure(true).UseNoneCommandParameter(true)//调试sql语句输出.UseMonitorCommand(cmd => System.Console.WriteLine(cmd.CommandText))
#endif;
});
builder.Services.AddSingleton(typeof(FreeSqlDataService<>));builder.Services.AddTransient<ImportExportsService>();
builder.Services.AddDensenExtensions();
builder.Services.ConfigureJsonLocalizationOptions(op =>
{// 忽略文化信息丢失日志op.IgnoreLocalizerMissing = true;
});

管理页面

Pages 添加组件 DataAdmin.razor

@page "/DataAdmin"
@using b16blazorIDS2.Models.ids 
@using static Blazor100.Service.ImportExportsService<PageTitle>管理</PageTitle><Tab IsLazyLoadTabItem="true"><TabItem Text="Users"><Table TItem="AspNetUsers"IsPagination="true"IsStriped="true"IsBordered="true"AutoGenerateColumns="true"ShowSearch="true"ShowToolbar="true"ShowExtendButtons="true"DoubleClickToEdit=trueShowColumnList=trueShowCardView=true><TableToolbarTemplate><TableToolbarButton TItem="AspNetUsers" Color="Color.Primary" Text="自由编辑" OnClick="@IsExcelToggle" /></TableToolbarTemplate></Table></TabItem><TabItem Text="Roles"><Table TItem="AspNetRoles"IsPagination="true"IsStriped="true"IsBordered="true"AutoGenerateColumns="true"ShowSearch="true"ShowToolbar="true"ShowExtendButtons="true"DoubleClickToEdit=trueShowColumnList=trueShowCardView=true><TableToolbarTemplate><TableToolbarButton TItem="AspNetRoles" Color="Color.Primary" Text="自由编辑" OnClick="@IsExcelToggle" /></TableToolbarTemplate></Table></TabItem><TabItem Text="Logins"><Table TItem="AspNetUserLogins"IsPagination="true"IsStriped="true"IsBordered="true"AutoGenerateColumns="true"ShowSearch="true"ShowToolbar="true"ShowExtendButtons="true"DoubleClickToEdit=trueShowColumnList=trueShowCardView=true><TableToolbarTemplate><TableToolbarButton TItem="AspNetUserLogins" Color="Color.Primary" Text="自由编辑" OnClick="@IsExcelToggle" /></TableToolbarTemplate></Table></TabItem></Tab>

组件 DataAdmin.razor 后置代码 DataAdmin.razor.cs

using Blazor100.Service;
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using System.Diagnostics.CodeAnalysis;namespace b16blazorIDS2.Pages
{public partial class DataAdmin{[Inject]IWebHostEnvironment? HostEnvironment { get; set; }[Inject][NotNull]NavigationManager? NavigationManager { get; set; }[Inject][NotNull]ImportExportsService? ImportExportsService { get; set; }[Inject][NotNull]ToastService? ToastService { get; set; } // 由于使用了FreeSql ORM 数据服务,可以直接取对象[Inject][NotNull]IFreeSql? fsql { get; set; }[Inject] ToastService? toastService { get; set; }[Inject] SwalService? SwalService { get; set; }public bool IsExcel { get; set; }public bool DoubleClickToEdit { get; set; } = true;protected string UploadPath = "";protected string? uploadstatus;long maxFileSize = 1024 * 1024 * 15;string? tempfilename;private Task IsExcelToggle(){IsExcel = !IsExcel;DoubleClickToEdit = !IsExcel;StateHasChanged();return Task.CompletedTask;}}
}

运行截图

本节源码

https://github.com/densen2014/Blazor100/tree/Blazor-%E6%95%99%E7%A8%8B15-6/b16blazorIDS2

源代码

https://github.com/densen2014/Blazor100

https://gitee.com/densen2014/Blazor100 (镜像/非最新版)—

关联项目

FreeSql QQ群:4336577

BA & Blazor QQ群:795206915

Maui Blazor 中文社区 QQ群:645660665

知识共享许可协议

本作品采用 知识共享署名-非商 业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名AlexChow,不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系 。

转载声明

本文来自博客园,作者:周创琳 AlexChow,转载请注明原文链接.

AlexChow

今日头条 | 博客园 | 知乎 | Gitee | GitHub

image

相关文章:

Blazor入门100天 : 身份验证和授权 (6) - 使用 FreeSql orm 管理ids数据

目录 建立默认带身份验证 Blazor 程序角色/组件/特性/过程逻辑DB 改 Sqlite将自定义字段添加到用户表脚手架拉取IDS文件,本地化资源freesql 生成实体类,freesql 管理ids数据表初始化 Roles,freesql 外键 > 导航属性完善 freesql 和 bb 特性 本节源码 https://github.com/…...

Java文件IO操作:File类的相关内容

Java文件IO操作一、File类1.相对路径和绝对路径2.路径分隔符&#xff08;同一路径下、多个路径下&#xff09;3.实例化4.常见方法一、File类 File类继承自Object类&#xff0c;实现了Serializable接口和Comparable接口&#xff1b; File类属于java.io包&#xff1b; File类是文…...

竣达技术 | 巡检触摸屏配合电池柜,电池安全放首位!

机房蓄电池常见的故障 1.机房电池着火和爆炸 目前在数据机房蓄电池爆炸着火事故频发&#xff0c;导致业主损失严重。一般机房电池是由于其中一节电池裂化后未妥善管理&#xff0c;电池急剧恶化导致爆炸着火。由于电池是串联及并联在使用&#xff0c;只要一节着火燃烧整片瞬间…...

什么是自动化运维?为什么选择Python做自动化运维?

“Python自动化运维”这个词&#xff0c;想必大家都听说过&#xff0c;但是很多人对它并不了解&#xff0c;也不知道是做什么的&#xff0c;那么你对Python自动化运维了解多少呢?跟着蛋糕往下看。 什么是Python自动化运维? 随着技术的进步、业务需求的快速增长&#xff0c;…...

【经验】移植环境requirement时报错

问题描述 在使用pip freeze > ./requirements.txt和pip install -r requirement.txt &#xff08;requirements.txt文件用来记录当前程序的所有依赖包及其精确版本号&#xff09;从一台电脑移植到另一台电脑的 conda 环境时&#xff0c;出现了一堆类似的报错&#xff1a; E…...

计算机专业要考什么证书?

大家好&#xff0c;我是良许。 从去年 12 月开始&#xff0c;我已经在视频号、抖音等主流视频平台上连续更新视频到现在&#xff0c;并得到了不错的评价。 视频 100% 原创录制&#xff0c;绝非垃圾搬运号&#xff0c;每个视频都花了很多时间精力用心制作&#xff0c;欢迎大家…...

一个列表引发的思考(简单版)

最近老板让我按照设计图写一个页面&#xff0c;不嫌丢人的说这是我第一次写页面&#xff0c;哈哈哈。 然后设计图里有一个这样的需求&#xff0c;感觉挺有意思的。 为什么感觉有意思呢&#xff0c;因为这个列表它前面是图片&#xff0c;然后单行和双行的不一样。&#xff08;请…...

Protobuf 学习简记(三)Unity C#中的序列化与反序列化

Protobuf 学习简记&#xff08;三&#xff09;Unity C#中的序列化与反序列化对文本的序列化与反序列化内存二进制流的序列化与反序列化方法一方法二参考链接对文本的序列化与反序列化 private void Text() {TestMsg1 myTestMsg new TestMsg1();myTestMsg.TestInt32 1;myTest…...

Flask入门(10):Flask使用SQLAlchemy

目录11.SQLAlchemy11.1 简介11.2 安装11.3 基本使用11.4 连接11.5 数据类型11.6 执行原生sql11.7 插入数据11. 8 删改操作11.9 查询11.SQLAlchemy 11.1 简介 SQLAlchemy的是Python的SQL工具包和对象关系映射&#xff0c;给应用程序开发者提供SQL的强大功能和灵活性。它提供了…...

我的 System Verilog 学习记录(4)

引言 本文简单介绍 System Verilog 语言的 数据类型。 前文链接&#xff1a; 我的 System Verilog 学习记录&#xff08;1&#xff09; 我的 System Verilog 学习记录&#xff08;2&#xff09; 我的 System Verilog 学习记录&#xff08;3&#xff09; 数据类型简介 Sys…...

Git : 本地分支与远程分支的映射关系

概述 本文介绍 git 环境中本地分支与远程分支的映射关系的查看和调整。 1、查看本地分支与远程分支的映射关系 执行如下命令&#xff1a; git branch -vv注意就是两个 v &#xff0c;没有写错。 可以获得分支映射结果&#xff1a; dev fa***** [github/dev] update * main…...

运维必看|跨国公司几千员工稳定访问Office365,怎么实现?

【客户背景】本次分享的客户是全球传感器领域的领导者&#xff0c;其核心产品为电流和电压传感器&#xff0c;被广泛应用于驱动和焊接、可再利用能源以及电源、牵引、高精度、传统和新能源汽车等领域。 作为一家中等规模的全球化公司&#xff0c;该公司在北京、日本、西欧、东欧…...

Python GDAL读取栅格数据并基于质量评估波段QA对指定数据加以筛选掩膜

本文介绍基于Python语言中gdal模块&#xff0c;对遥感影像数据进行栅格读取与计算&#xff0c;同时基于QA波段对像元加以筛选、掩膜的操作。本文所要实现的需求具体为&#xff1a;现有自行计算的全球叶面积指数&#xff08;LAI&#xff09;.tif格式栅格产品&#xff08;下称“自…...

Vue3:有关v-model的用法

目录 前言&#xff1a; 回忆基本的原生用法&#xff1a; 原生input的封装&#xff1a; 自定义v-model参数&#xff1a; 对el-input的二次封装&#xff1a; 多个v-model进行绑定: v-model修饰符&#xff1a; v-model自定义参数与自定义修饰符的结合&#xff1a; 前言&am…...

CF1692C Where‘s the Bishop? 题解

CF1692C Wheres the Bishop? 题解题目链接字面描述题面翻译题目描述题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1提示代码实现题目 链接 https://www.luogu.com.cn/problem/CF1692C 字面描述 题面翻译 题目描述 有一个888\times888的棋盘&#xff0c;列编号从…...

Jenkins集成Allure报告

Jenkins集成Allure报告 紧接上文&#xff1a;Jenkins部署及持续集成——傻瓜式教程 使用Allure报告 1、在插件库下载Allure插件Allure Jenkins Plugin 2、在构建后操作中加入allure执行的报告目录&#xff08;相对于项目的路径&#xff09; 3、run.py代码改成如下 import p…...

【数据结构】AVL树

AVL树一、AVL树的概念二、AVL的接口2.1 插入2.2 旋转2.2.1 左单旋2.2.2 右单旋2.2.3 左右双旋2.2.4 右左双旋三、验证四、源码一、AVL树的概念 当我们用普通的搜索树插入数据的时候&#xff0c;如果插入的数据是有序的&#xff0c;那么就退化成了一个链表&#xff0c;搜索效率…...

这一次我不再低调,老板法拉利的车牌有我的汗水

起源: 5Why分析法最初是由丰田佐吉提出的,后来,丰田汽车公司在发展完善其制造方法学的过程中持续采用该方法。5Why分析法作为丰田生产系统的入门课程之一,是问题求解的关键培训课程。丰田生产系统的设计师大野耐一曾将5Why分析法描述为:“丰田科学方法的基础,重复五次,问…...

通过连接另一个数组的子数组得到一个数组

给你一个长度为 n 的二维整数数组 groups &#xff0c;同时给你一个整数数组 nums 。 你是否可以从 nums 中选出 n 个 不相交 的子数组&#xff0c;使得第 i 个子数组与 groups[i] &#xff08;下标从 0 开始&#xff09;完全相同&#xff0c;且如果 i > 0 &#xff0c;那么…...

公派访问学者的申请条件

知识人网海外访问学者申请老师为大家分享公派访问学者申请的基本条件以及哪些人员的申请是暂不受理的&#xff0c;供大家参考&#xff1a;一、 申请人基本条件&#xff1a;1.热爱社会主义祖国&#xff0c;具有良好的思想品德和政治素质&#xff0c;无违法违纪记录。2.具有良好专…...

Python|GIF 解析与构建(5):手搓截屏和帧率控制

目录 Python&#xff5c;GIF 解析与构建&#xff08;5&#xff09;&#xff1a;手搓截屏和帧率控制 一、引言 二、技术实现&#xff1a;手搓截屏模块 2.1 核心原理 2.2 代码解析&#xff1a;ScreenshotData类 2.2.1 截图函数&#xff1a;capture_screen 三、技术实现&…...

[2025CVPR]DeepVideo-R1:基于难度感知回归GRPO的视频强化微调框架详解

突破视频大语言模型推理瓶颈,在多个视频基准上实现SOTA性能 一、核心问题与创新亮点 1.1 GRPO在视频任务中的两大挑战 ​安全措施依赖问题​ GRPO使用min和clip函数限制策略更新幅度,导致: 梯度抑制:当新旧策略差异过大时梯度消失收敛困难:策略无法充分优化# 传统GRPO的梯…...

以下是对华为 HarmonyOS NETX 5属性动画(ArkTS)文档的结构化整理,通过层级标题、表格和代码块提升可读性:

一、属性动画概述NETX 作用&#xff1a;实现组件通用属性的渐变过渡效果&#xff0c;提升用户体验。支持属性&#xff1a;width、height、backgroundColor、opacity、scale、rotate、translate等。注意事项&#xff1a; 布局类属性&#xff08;如宽高&#xff09;变化时&#…...

Java如何权衡是使用无序的数组还是有序的数组

在 Java 中,选择有序数组还是无序数组取决于具体场景的性能需求与操作特点。以下是关键权衡因素及决策指南: ⚖️ 核心权衡维度 维度有序数组无序数组查询性能二分查找 O(log n) ✅线性扫描 O(n) ❌插入/删除需移位维护顺序 O(n) ❌直接操作尾部 O(1) ✅内存开销与无序数组相…...

Linux相关概念和易错知识点(42)(TCP的连接管理、可靠性、面临复杂网络的处理)

目录 1.TCP的连接管理机制&#xff08;1&#xff09;三次握手①握手过程②对握手过程的理解 &#xff08;2&#xff09;四次挥手&#xff08;3&#xff09;握手和挥手的触发&#xff08;4&#xff09;状态切换①挥手过程中状态的切换②握手过程中状态的切换 2.TCP的可靠性&…...

智能在线客服平台:数字化时代企业连接用户的 AI 中枢

随着互联网技术的飞速发展&#xff0c;消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁&#xff0c;不仅优化了客户体验&#xff0c;还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用&#xff0c;并…...

2021-03-15 iview一些问题

1.iview 在使用tree组件时&#xff0c;发现没有set类的方法&#xff0c;只有get&#xff0c;那么要改变tree值&#xff0c;只能遍历treeData&#xff0c;递归修改treeData的checked&#xff0c;发现无法更改&#xff0c;原因在于check模式下&#xff0c;子元素的勾选状态跟父节…...

华为OD机试-食堂供餐-二分法

import java.util.Arrays; import java.util.Scanner;public class DemoTest3 {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) { // 注意 while 处理多个 caseint a in.nextIn…...

Module Federation 和 Native Federation 的比较

前言 Module Federation 是 Webpack 5 引入的微前端架构方案&#xff0c;允许不同独立构建的应用在运行时动态共享模块。 Native Federation 是 Angular 官方基于 Module Federation 理念实现的专为 Angular 优化的微前端方案。 概念解析 Module Federation (模块联邦) Modul…...

从零实现STL哈希容器:unordered_map/unordered_set封装详解

本篇文章是对C学习的STL哈希容器自主实现部分的学习分享 希望也能为你带来些帮助~ 那咱们废话不多说&#xff0c;直接开始吧&#xff01; 一、源码结构分析 1. SGISTL30实现剖析 // hash_set核心结构 template <class Value, class HashFcn, ...> class hash_set {ty…...