本文介绍如何将项目中已存在的【实体类】,直接生产出 CRUD 后台管理界面。
Song、Tag 多对多场景,添加/更新 Song 时可以把 Tag 一起保存;
列表页,希望枚举出现在过滤筛选条件;
等等诸如此类的繁琐操作,之所以说繁琐,是因为这些工作技术不难,属于严重的重复劳动。
演示功能
Domain 目录下的文件是【实体类】
Program.cs 是 asp.net core 启动代码
using FreeSql;
using ojbk.Entities;
var fsql = new FreeSql.FreeSqlBuilder(
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=test.db;Pooling=true;Max Pool Size=10"
.UseAutoSyncStructure(true
.UseNoneCommandParameter(true
.UseMonitorCommand(cmd => Console.WriteLine(cmd.CommandText + "\r\n"
.Build(;
var builder = WebApplication.CreateBuilder(args;
builder.Services.AddControllersWithViews(;
builder.Services.AddSingleton(fsql;
builder.Services.AddEndpointsApiExplorer(;
builder.Services.AddSwaggerGen(;
var app = builder.Build(;
if (app.Environment.IsDevelopment(
{
app.UseSwagger(;
app.UseSwaggerUI(;
}
app.UseAuthorization(;
app.MapControllers(;
app.UseDefaultFiles(;
app.UseStaticFiles(;
/******************************* 以下是核心代码 **************************/
app.UseFreeAdminLtePreview("/",
typeof(Config,
typeof(Role,
typeof(Menu,
typeof(User,
typeof(Department,
typeof(Employee,
typeof(Position,
typeof(AppLog,
typeof(LoginLog,
typeof(OprationLog,
typeof(IdleScheduler.TaskInfo,
typeof(IdleScheduler.TaskLog
;
app.Run(;
既然 ORM 已经对实体进行了配置,利用已存在的条件便利的产生后台管理功能,真是一大快事!
输入:实体1、实体2、实体3
只需要传入实体,就可以生产 curd 的管理功能,是不是有些骚啊~~~
机制设定
1、添加、修改数据
比较特殊的映射规则:
c# 类型 | Html5 |
---|---|
布尔 | 复选框 |
枚举 | 下拉选择 |
日期 | 日期控件 |
ManyToOne 导航属性 | 下拉选择 |
ManyToMany 导航属性 | 多选器 |
什么情况会产生【上传文件】控件?
有兴趣的可以了解源码,目前没有开放在外部配置。
中件间为每个实体提供了分页列表查询,每页为20条数据;
3、删除数据
并且支持了复杂导航属性关系的级联删除功能,而这个功能不依赖数据库外键;
思考人生
一番惊喜过后,你应该会考虑实用性,这样做有什么价值,可用于什么样的场景?
思路方向应该不会有问题,因为真的太方便了,如果生成后的代码是 Service + Vue + ElementUI,并且还提供了默认通用权限、定时任务等功能,就问香不香?
若有使用疑问请留言,谢谢!
github:https://github.com/2881099
https://github.com/dotnetcore/FreeSql.AdminLTE