NET Core 6 .0 配置 SqlSugar
选中项目NuGet包管理安装SqlSugarCore
安装好后>在配置文件中创建数据库连接字符串
创建一个SqlSugarContext
using RBACHS_Domain;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace RBACHS_IRepository
{
public class SqlSugarContext
{
public readonly ISqlSugarClient db;
public SqlSugarContext(ISqlSugarClient db )
{
this.db = db;
}
public void CreateTable()
{
db.DbMaintenance.CreateDatabase();//没有数据库则新建
db.CodeFirst.SetStringDefaultLength(50).BackupTable().InitTables(new Type[]
{
typeof(Aundit),
typeof(ClientInfo),
typeof(Dictionary),
typeof(District),
typeof(Menu),
typeof(Project),
typeof(Role),
typeof(RoleMenu),
typeof(Survey),
typeof(User),
typeof(UserRole),
});
}
}
}
把需要迁移的视图 放到里面 然后在Program启动项里面 加入上下文
//注册上下文:AOP里面可以获取IOC对象,如果有现成框架比如Furion可以不写这一行
builder.Services.AddHttpContextAccessor();
//注册SqlSugar用AddScoped
builder.Services.AddScoped<ISqlSugarClient>(s =>
{
//Scoped用SqlSugarClient
SqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.MySql,
ConnectionString = builder.Configuration.GetConnectionString("MySqlHSFC"),
IsAutoCloseConnection = true,
},
db =>
{
});
return sqlSugar;
});
最后在接口里面 生成 创建 SqlSugarContext 的构造函数
最后写一个迁移方法
最后返回hehe 就完成了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)