一 根据实体类迁移创建表
  1. 在项目NuGet中安装SqlSugar和System.Data.SqlClient
    在这里插入图片描述

  2. 创建实体类,存放在Models文件夹下
    在这里插入图片描述
    CodeFirstTable1表实体类

using SqlSugar;

namespace WebApplication2.Models
{
    public class CodeFirstTable1
    {
        [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
        public int Id { get; set; }
        public string Name { get; set; }
        [SugarColumn(ColumnDataType = "Nvarchar(255)")]//自定格式的情况 length不要设置
        public string Text { get; set; }
        [SugarColumn(IsNullable = true)]//可以为NULL
        public DateTime CreateTime { get; set; }
    }
}

test表实体类

using SqlSugar;

namespace WebApplication2.Models
{
    public class test
    {
        [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

在这里插入图片描述
3. 封装迁移创建表和库的方法,新建一个Context文件夹,里面新建一个SqlSugarContext类,用于封装迁移表方法

using SqlSugar;
using WebApplication2.Models;

namespace WebApplication2.Context
{
    public class SqlSugarContext
    {
        public readonly ISqlSugarClient db;
        public SqlSugarContext(ISqlSugarClient DBContext)
        {
            this.db = DBContext;
        }
        public void CreateTable()
        {
            db.DbMaintenance.CreateDatabase();//没有数据库则新建
            db.CodeFirst.SetStringDefaultLength(50).BackupTable().InitTables(new Type[]
            {
               typeof(CodeFirstTable1),         //根据CodeFirstTable1实体创建表
               typeof(test)                     //根据test实体创建表
            });
        }
    }
}

在这里插入图片描述

  1. 添加数据库连接字符串,注册服务
    在appsettings.json添加数据库连接字符串
  "ConnectionStrings": {
    "DbConnectionString": "Data Source = LAPTOP-RMDBADPQ\\NING;Initial Catalog = myDataBasetest;User Id = sa;Password = 123456;"
  }

在这里插入图片描述
在Program类给刚才封装好的类注册sqlsugar服务

builder.Services.AddSingleton(sp => new SqlSugarContext(
    new SqlSugarClient(new ConnectionConfig()
    {
        ConnectionString = builder.Configuration.GetConnectionString("DbConnectionString"), //数据库连接串
        DbType = DbType.SqlServer,      //数据库类型
        IsAutoCloseConnection = true //自动释放
    })
));
//builder.Services.AddScoped(options =>
//{
//    return new SqlSugarClient(new ConnectionConfig()
//    {
//        ConnectionString = builder.Configuration.GetConnectionString("DbConnectionString"),
//        DbType = DbType.SqlServer,
//        IsAutoCloseConnection = true,
//        InitKeyType = InitKeyType.Attribute
//    });

//    //return new SqlSugarClient(new List<ConnectionConfig>()
//    //{
//    //    new ConnectionConfig() {
//    //        //ConfigId = DBEnum.默认数据库, 
//    //        ConnectionString =builder.Configuration.GetConnectionString("DbConnectionString"),
//    //        //DbType = DbType.MySql, 
//    //        DbType = DbType.SqlServer,
//    //        IsAutoCloseConnection = true //自动释放
//    //    } //多个库就传List<IocConfig>
//    //});
//});

在这里插入图片描述
5. 新建api控制器,新建接口调用迁移创建表方法

using Microsoft.AspNetCore.Mvc;
using WebApplication2.Context;

namespace WebApplication2.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        public readonly SqlSugarContext db;
        public ValuesController(SqlSugarContext DBContext)
        {
            this.db = DBContext;
        }
        [HttpGet("Create")]
        public string CreateTable()
        {
            db.CreateTable();
            return "hehe";
        }
    }
}

在这里插入图片描述
6. 运行接口,创建成功
在这里插入图片描述
在这里插入图片描述

二 根据数据库表生成实体类
  1. 安装NuGet包 Newtonsoft.Json
    在这里插入图片描述
  2. 在 一 中封装的SqlSugarContext类中添加生成实体方法
 /// <summary>
        /// //参数1:路径  参数2:命名空间
        //IsCreateAttribute 代表生成SqlSugar特性
        /// </summary>
        public void CreateClassFile()
        {
            db.DbFirst.IsCreateAttribute().CreateClassFile("D:\\Demo\\1", "Models");
        }

在这里插入图片描述
3. 添加api接口调用运行此方法

 [HttpGet("CreateClassFile")]
        public bool CreateClassFile()
        {
            db.CreateClassFile();
            return true;
        }

在这里插入图片描述
4. 运行项目,调用此api接口,下面运行结果成功
在这里插入图片描述
查看生成的实体类,成功
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐