6-引入SqlSugar

首页 / WebApi / 正文

使用SqlSugar 连接操作数据库

为什么使用sqlSugar而不是EF Core
因为SqlSugar相对简单,配置少,上手容易,对新手更加友好

官方文档地址

https://www.donet5.com/Home/Doc

Nuget 安装SqlSugarCore

如果忘记了可以去看官方文档

依赖注入

两种方式 具体看文档,这里只介绍Net 自带的IOC

SqlSugarScope sqlSugar = new SqlSugarScope(
    new ConnectionConfig()
    {
        DbType = SqlSugar.DbType.SqlServer,
        ConnectionString = "",
        IsAutoCloseConnection = true,
    },
        db =>
        {
            //单例参数配置,所有上下文生效
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                //Console.WriteLine(sql);//输出sql
            };
        });
services.AddSingleton<ISqlSugarClient>(sqlSugar);

这里主要视频演示吧,官方文档比我写的要好

CodeFirst

具体参数还是看官方文档

//建库
db.DbMaintenance.CreateDatabase();
//建表
db.CodeFirst.InitTables(typeof(CodeFirstTable1));
无标签
评论区
头像