基于.NET6平台开发WebApi(七)—— 添加Sqlsugar支持

源码点我

1、Sqlsugar配置完毕,我们按照如下结构来创建实体类,为了方便开发,我们先创建一个实体基类EntityBase,然后创建Address类继承于EntityBase
基于.NET6平台开发WebApi(七)—— 添加Sqlsugar支持_第1张图片

using SqlSugar;

namespace NET6.Domain.Entities
{
    /// 
    /// 实体基类
    /// 
    public class EntityBase
    {
        /// 
        /// 编号
        /// 
        [SugarColumn(IsPrimaryKey = true, ColumnDescription = "主键")]
        public string Id { get; set; } = Guid.NewGuid().ToString("N");
        /// 
        /// 是否删除
        /// 
        [SugarColumn(ColumnDescription = "是否删除")]
        public bool IsDeleted { get; set; } = false;
        /// 
        /// 创建者Id
        /// 
        [SugarColumn(ColumnDescription = "创建者Id")]
        public string CreateUserId { get; set; }
        /// 
        /// 创建时间
        /// 
        [SugarColumn(ColumnDescription = "创建时间")]
        public DateTime CreateTime { get; set; } = DateTime.Now;
        /// 
        /// 删除者Id
        /// 
        [SugarColumn(ColumnDescription = "删除者Id")]
        public string DeleteUserId { get; set; }
        /// 
        /// 删除时间
        /// 
        [SugarColumn(ColumnDescription = "删除时间")]
        public DateTime DeleteTime { get; set; }
    }
}
using SqlSugar;

namespace NET6.Domain.Entities
{
    /// 
    /// 地址
    /// 
    [SugarTable("address")]
    public class Address : EntityBase
    {
        /// 
        /// 用户ID
        /// 
        public string UserId { get; set; }
        /// 
        /// 收件人姓名
        /// 
        public string Name { get; set; }
        /// 
        /// 收件人手机号
        /// 
        public string Phone { get; set; }
        /// 
        /// 省份
        /// 
        public string Province { get; set; }
        /// 
        /// 城市
        /// 
        public string City { get; set; }
        /// 
        /// 区域
        /// 
        public string Area { get; set; }
        /// 
        /// 详细地址
        /// 
        public string Detail { get; set; }
        /// 
        /// 是否是默认地址
        /// 
        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设置为启动项目,运行,打开数据库会发现数据表已经创建完成。
基于.NET6平台开发WebApi(七)—— 添加Sqlsugar支持_第2张图片

你可能感兴趣的:(个人随笔,.net,c#,后端)