赞
踩
SqlSugarCore
public static class SqlsugarSetup
{
public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration,
string dbName = "db_master")
{
SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
{
DbType = SqlSugar.DbType.MySql,
ConnectionString = configuration[dbName],
IsAutoCloseConnection = true,
},
db =>
{
//单例参数配置,所有上下文生效
db.Aop.OnLogExecuting = (sql, pars) =>
{
//Console.WriteLine(sql);//输出sql
};
});
services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
}
}
"db_master": "server=106.75.6.169;port=3306;user=root;password=password;database=minecraftdb"
using SqlSugar;
namespace WebApiStudy_3_3.Models
{
[SugarTable("T_User")]
public class User
{
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
public int Id { get; set; }
[SugarColumn(ColumnName="Name")]
public string UserName { get; set; }
public int Age { get; set; }
}
}
namespace WebApiStudy_3_3.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DbtestController : ControllerBase
{
public DbtestController(ISqlSugarClient db)
{
Db = db;
}
private ISqlSugarClient Db { get; }
/// <summary>
///
/// </summary>
[HttpPost]
public bool CreateDb()
{
return Db.DbMaintenance.CreateDatabase();
}
/// <summary>
///
/// </summary>
[HttpPost("Table")]
public void CreateTable()
{
Db.CodeFirst.InitTables(typeof(User));
}
/// <summary>
///
/// </summary>
[HttpGet]
public List<User> GetTable()
{
return Db.Queryable<User>().ToList();
}
}
}
三个请求分别是,创建数据库,创建表,获取全部数据。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。