2025-07-19 09:25:01 +08:00
|
|
|
using DMS.Config;
|
2025-07-19 11:11:01 +08:00
|
|
|
using DMS.Infrastructure.Interfaces;
|
2025-07-18 19:56:00 +08:00
|
|
|
using SqlSugar;
|
2025-07-19 09:25:01 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
2025-05-29 08:58:58 +08:00
|
|
|
|
2025-07-19 09:25:01 +08:00
|
|
|
namespace DMS.Infrastructure.Data;
|
2025-06-23 17:01:06 +08:00
|
|
|
|
2025-07-19 11:11:01 +08:00
|
|
|
public class SqlSugarDbContext : ITransaction
|
2025-05-29 08:58:58 +08:00
|
|
|
{
|
2025-07-19 09:25:01 +08:00
|
|
|
private readonly SqlSugarClient _db;
|
|
|
|
|
|
|
|
|
|
public SqlSugarDbContext(ConnectionSettings settings)
|
2025-06-23 17:01:06 +08:00
|
|
|
{
|
2025-07-06 11:09:57 +08:00
|
|
|
var connectionString = settings.ToConnectionString();
|
|
|
|
|
var dbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), settings.DbType);
|
|
|
|
|
|
2025-07-19 09:25:01 +08:00
|
|
|
_db = new SqlSugarClient(new ConnectionConfig
|
2025-05-29 08:58:58 +08:00
|
|
|
{
|
2025-06-25 22:33:57 +08:00
|
|
|
ConnectionString = connectionString,
|
2025-07-19 09:25:01 +08:00
|
|
|
DbType = dbType,
|
2025-06-25 22:33:57 +08:00
|
|
|
IsAutoCloseConnection = true,
|
|
|
|
|
InitKeyType = InitKeyType.Attribute
|
|
|
|
|
});
|
2025-07-19 09:25:01 +08:00
|
|
|
}
|
2025-06-25 22:33:57 +08:00
|
|
|
|
2025-07-19 09:25:01 +08:00
|
|
|
|
|
|
|
|
public async Task BeginTranAsync()
|
|
|
|
|
{
|
|
|
|
|
await _db.BeginTranAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task CommitTranAsync()
|
|
|
|
|
{
|
|
|
|
|
await _db.CommitTranAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 11:11:01 +08:00
|
|
|
|
|
|
|
|
|
2025-07-19 09:25:01 +08:00
|
|
|
public async Task RollbackTranAsync()
|
|
|
|
|
{
|
|
|
|
|
await _db.RollbackTranAsync();
|
|
|
|
|
}
|
2025-07-19 11:11:01 +08:00
|
|
|
|
|
|
|
|
public SqlSugarClient GetInstance()
|
|
|
|
|
{
|
|
|
|
|
return _db;
|
|
|
|
|
}
|
2025-06-23 17:01:06 +08:00
|
|
|
}
|