.net6 下 SqlSugar迁移创建表以及生成实体类 sqlserver数据库

4 篇文章 0 订阅
1 篇文章 0 订阅
一 根据实体类迁移创建表
  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接口,下面运行结果成功
在这里插入图片描述
查看生成的实体类,成功
在这里插入图片描述

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值