40 lines
1.2 KiB
C#
Raw Normal View History

2025-04-06 13:36:16 +08:00
using SqlSugar;
using System;
namespace linesider_screen_bankend.Services.SQLHepler
{
public static class SqlSugarHelper
{
private static readonly Lazy<SqlSugarScope> _db = new Lazy<SqlSugarScope>(() =>
{
var config = new ConnectionConfig()
{
2025-04-11 09:50:16 +08:00
ConnectionString = "Data Source=192.168.50.163;User ID=root;Password=123456;Initial Catalog=GXAssembly;Port=3306", // 替换为你的MySQL连接字符串
2025-04-06 13:36:16 +08:00
DbType = DbType.MySql, // 数据库类型设置为MySQL
IsAutoCloseConnection = true, // 自动释放
InitKeyType = InitKeyType.Attribute // 从实体特性读取主键信息
};
var scope = new SqlSugarScope(config, db =>
{
// 配置AOP
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql); // 输出SQL
// 可以在这里记录日志
};
// 配置更多选项
db.Ado.CommandTimeOut = 30;
});
return scope;
});
public static SqlSugarScope Db => _db.Value;
}
}