赞
踩
目录
SqlSugar是一款老牌 .NET 开源ORM框架。
主要特点:简单易用、功能齐全、高性能、轻量级、服务齐全、支持全自动分表组件,SAAS分库,大数据处理的ORM。
安装包: SqlSugarCore
代码如下(示例):
- [SugarTable("T_Book")]//设置表名为TableName,如果不设置取类名为表名
- public class Book
- {
-
- [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
- public int Id { get; set; }
-
- [SugarColumn(Length =50)]//设置长度
- public string AuthorName { get; set; }
- public float?Price { get; set; }
-
- [SugarColumn(IsNullable = true)]//可以为NULL
- public DateTime PubTime { get; set; }
- public string Title { get; set; }
- }
代码如下(示例):
- public class SqlSugarContext
- {
-
- public readonly ISqlSugarClient db;
-
- public SqlSugarContext(ISqlSugarClient db)
- {
- this.db = db;
- }
-
- public void CreateTable()
- {
- //建库 如果不存在创建数据库,存在不会重复创建
- db.DbMaintenance.CreateDatabase();
- //建表
- db.CodeFirst.SetStringDefaultLength(100).//设置默认字符串类型的字段长度
- BackupTable().InitTables(new Type[] //支持多表创建
- {
- typeof(Book),
- });
- }
- }
代码如下(示例):
- builder.Services.AddHttpContextAccessor();
- //注册SqlSugar用AddScoped
- builder.Services.AddScoped(sp => new SqlSugarContext(
- new SqlSugarClient(new ConnectionConfig()
- {
- ConnectionString = builder.Configuration.GetConnectionString("SqlServer:Connection"), //数据库连接串
- DbType = DbType.SqlServer, //数据库类型
- IsAutoCloseConnection = true, //自动释放
- MoreSettings = new ConnMoreSettings()
- {
- SqlServerCodeFirstNvarchar = true,//建表字符串默认Nvarchar
- }
- },
- db =>
- {
- db.Aop.OnLogExecuting = (sql, pars) =>
- {
- Console.WriteLine(sql);//生成执行Sql语句
- };
- }
-
- )
- ));
代码如下(示例):
- [Route("api/[controller]/[action]")]
- [ApiController]
- public class InitializationController : ControllerBase
- {
-
- public readonly SqlSugarContext _context;
- public InitializationController(SqlSugarContext context) {
-
- this._context = context;
- }
-
- /// <summary>
- /// 创建表
- /// </summary>
- /// <returns></returns>
- [HttpGet]
- public IActionResult CreateTable( )
- {
- _context.CreateTable();
- return Ok("执行成功!");
- }
- }
运行项目,执行创建表的接口
再去数据库看,库和表都建立好了
如果想增加新的字段、修改字段,建新实体再次执行该接口即可
代码如下(示例):
- [Route("api/[controller]/[action]")]
- [ApiController]
- public class BookController : ControllerBase
- {
-
- public readonly SqlSugarContext _context;
- public BookController(SqlSugarContext context) {
-
- this._context = context;
- }
-
- /// <summary>
- /// 新增
- /// </summary>
- /// <returns></returns>
- [HttpPost]
- public IActionResult InsertBook(Book req)
- {
- int result= _context.db.Insertable<Book>(req).ExecuteCommand();
- return Ok($"成功插入{result}条");
- }
-
- /// <summary>
- /// 编辑
- /// </summary>
- /// <returns></returns>
- [HttpPost]
- public IActionResult UpdateBook(Book req)
- {
- int result = _context.db.Updateable<Book>(req).ExecuteCommand();
- return Ok($"成功修改{result}条");
- }
-
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="Id"></param>
- /// <returns></returns>
- [HttpGet]
- public IActionResult DeleteBook(int Id)
- {
-
- int result = _context.db.Deleteable<Book>().In(Id).ExecuteCommand();
- return Ok($"成功删除{result}条");
- }
-
- /// <summary>
- /// 查询
- /// </summary>
- /// <param name="pagenumber"></param>
- /// <param name="pageSize"></param>
- /// <returns></returns>
- [HttpGet]
- public IActionResult QueryBook(int pagenumber=1, int pageSize=20) {
- int totalCount = 0;
- //单表分页
- var page = _context.db.Queryable<Book>().ToPageList(pagenumber, pageSize, ref totalCount);
- var data = new
- {
- page,
- totalCount
- };
- return Ok(data);
- }
- }
SqlSugar比起Migrationn数据迁移的方式建库建表,简化很多。ISqlSugarClient封装了常用的CURD功能,并且支持批量操作,在性能方面也不错,是很容易上手的一款orm框架。
更多使用方法查看官方文档
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。