NewGaoKaoApi/New_College.Repository/UnitOfWork/UnitOfWork.cs

52 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using New_College.IRepository.UnitOfWork;
using SqlSugar;
using System;
namespace New_College.Repository.UnitOfWork
{
public class UnitOfWork : IUnitOfWork
{
private readonly ISqlSugarClient _sqlSugarClient;
public UnitOfWork(ISqlSugarClient sqlSugarClient)
{
_sqlSugarClient = sqlSugarClient;
}
/// <summary>
/// 获取DB保证唯一性
/// </summary>
/// <returns></returns>
public SqlSugarClient GetDbClient()
{
// 必须要as后边会用到切换数据库操作
return _sqlSugarClient as SqlSugarClient;
}
public void BeginTran()
{
GetDbClient().BeginTran();
}
public void CommitTran()
{
try
{
GetDbClient().CommitTran(); //
}
catch (Exception ex)
{
GetDbClient().RollbackTran();
throw ex;
}
}
public void RollbackTran()
{
GetDbClient().RollbackTran();
}
}
}