栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Cocos2d-x

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

Cocos2d-x 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

一 根据实体类迁移创建表
  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()
//    //{
//    //    new ConnectionConfig() {
//    //        //ConfigId = DBEnum.默认数据库, 
//    //        ConnectionString =builder.Configuration.GetConnectionString("DbConnectionString"),
//    //        //DbType = DbType.MySql, 
//    //        DbType = DbType.SqlServer,
//    //        IsAutoCloseConnection = true //自动释放
//    //    } //多个库就传List
//    //});
//});


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类中添加生成实体方法
 /// 
        /// //参数1:路径  参数2:命名空间
        //IsCreateAttribute 代表生成SqlSugar特性
        /// 
        public void CreateClassFile()
        {
            db.DbFirst.IsCreateAttribute().CreateClassFile("D:\Demo\1", "Models");
        }


3. 添加api接口调用运行此方法

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


4. 运行项目,调用此api接口,下面运行结果成功

查看生成的实体类,成功

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/967827.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号