基于.NET6平台开发WebApi(七)—— 添加Sqlsugar支持
基于NET6平台WebApi尝鲜项目: 基于最新的.NET6平台,完全抛弃繁杂的接口,采用.NET最经典的三层架构,结合简化的DDD设计模式... (gitee.com)
1、Sqlsugar配置完毕,我们按照如下结构来创建实体类,为了方便开发,我们先创建一个实体基类EntityBase,然后创建Address类继承于EntityBase
using SqlSugar; namespace NET6.Domain.Entities { /// <summary> /// 实体基类 /// </summary> public class EntityBase { /// <summary> /// 编号 /// </summary> [SugarColumn(IsPrimaryKey = true, ColumnDescription = "主键")] public string Id { get; set; } = Guid.NewGuid().ToString("N"); /// <summary> /// 是否删除 /// </summary> [SugarColumn(ColumnDescription = "是否删除")] public bool IsDeleted { get; set; } = false; /// <summary> /// 创建者Id /// </summary> [SugarColumn(ColumnDescription = "创建者Id")] public string CreateUserId { get; set; } /// <summary> /// 创建时间 /// </summary> [SugarColumn(ColumnDescription = "创建时间")] public DateTime CreateTime { get; set; } = DateTime.Now; /// <summary> /// 删除者Id /// </summary> [SugarColumn(ColumnDescription = "删除者Id")] public string DeleteUserId { get; set; } /// <summary> /// 删除时间 /// </summary> [SugarColumn(ColumnDescription = "删除时间")] public DateTime DeleteTime { get; set; } } }
using SqlSugar; namespace NET6.Domain.Entities { /// <summary> /// 地址 /// </summary> [SugarTable("address")] public class Address : EntityBase { /// <summary> /// 用户ID /// </summary> public string UserId { get; set; } /// <summary> /// 收件人姓名 /// </summary> public string Name { get; set; } /// <summary> /// 收件人手机号 /// </summary> public string Phone { get; set; } /// <summary> /// 省份 /// </summary> public string Province { get; set; } /// <summary> /// 城市 /// </summary> public string City { get; set; } /// <summary> /// 区域 /// </summary> public string Area { get; set; } /// <summary> /// 详细地址 /// </summary> public string Detail { get; set; } /// <summary> /// 是否是默认地址 /// </summary> public bool IsDefault { get; set; } } }
2、为了更方便的使用数据库,我们为解决方案新增一个控制台项目,并使用CodeFirst来同步数据库表结构,新增控制台项目取名为EntitySync,打开Program.cs,添加如下代码
// See https://aka.ms/new-console-template for more information using NET6.Domain.Entities; using SqlSugar; var db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "server=localhost;Database=DBTest;Uid=root;Pwd=123456@q;", DbType = DbType.MySql, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); #region Aop //db.Aop.OnLogExecuting = (sql, pars) => //{ // Console.WriteLine(sql + "" + Db.Utilities.SerializeObject // (pars.ToDictionary(it => it.ParameterName, it => it.Value))); // Console.WriteLine(); //}; #endregion #region 对话框 Console.WriteLine("是否确定同步数据库表结构?(同名数据表将会被备份,生产环境慎用,回车确认)"); var str = Console.ReadKey(); if (str.Key == ConsoleKey.Enter) { Console.WriteLine("同步中,请稍后..."); } else { Console.WriteLine("\r\n输入错误,已退出..."); return; } #endregion //同步数据表结构 db.DbMaintenance.CreateDatabase(); db.CodeFirst.SetStringDefaultLength(50).BackupTable().InitTables(new Type[] { typeof(Address) }); Console.WriteLine("数据库结构同步完成!");
3、将EntitySync设置为启动项目,运行,打开数据库会发现数据表已经创建完成。
本文来自博客园,作者:昨夜丶雨疏风骤,转载请注明原文链接:https://www.cnblogs.com/netsix/p/15765500.html