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 就完成了

 

posted @   恒译  阅读(728)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示