#regionSaveChanges comes with its own transaction
///1. Context contains operations that can be performed on a database and must not be performed on a table. Multiple actions can be performed
///2. After SaveChanges, the transaction is started by default and all operations on the database are committed at one time. It's all success or all failure
using (TencentClassRoomContext context = new TencentClassRoomContext())
{
    {
        var userlist = context.SysUsers.Where(a => a.Id > 3).ToList();
        var user = context.SysUsers.Find(5);
    }
    {
        Company adCompany001 = new Company()
        {
            Name = "Test company 0001."
        };
        context.Companies.Add(adCompany001);
        Company adCompany002 = new Company()
        {
            Name = "Test company 0002~~"
        };
        context.Companies.Add(adCompany002);
    }
    {
        SysLog updatlog = context.SysLogs.Find(1);
        updatlog.Introduction += "---ABCDEFG";
    }
    {
        SysLog removelog2 = context.SysLogs.Find(2);
        context.SysLogs.Remove(removelog2);
    }
    context.SaveChanges();
}
#endregion

#regionEF executes SQL transactions;
//1.SaveChanges
//2.dbContext.Database.BeginTransaction();
using (TencentClassRoomContext dbContext = new TencentClassRoomContext())
{
    {
        DbContextTransaction trans = null;
        try
        {
            trans = dbContext.Database.BeginTransaction();
            string sql = "Update [SysUser] Set Name='Richard-0001' WHERE Id=@Id";
            SqlParameter parameter = new SqlParameter("@Id".1);
            dbContext.Database.ExecuteSqlCommand(sql, parameter);
            SysLog updatlog = dbContext.SysLogs.Find(1);
            updatlog.UserName = "-- -- -- --";
            updatlog.Introduction += "---ABCDEFG";
            dbContext.SaveChanges();
            trans.Commit();// A transaction is committed only when BeginTransaction Commit occurs;
        }
        catch (Exception ex)
        {
            if(trans ! =null)
                trans.Rollback();
            throw ex;
        }
        finally{ trans.Dispose(); }}}#endregion

#regionDistributed transaction
using (TencentClassRoomContext dbContext1 = new TencentClassRoomContext())
using (TencentClassRoomContext dbContext2 = new TencentClassRoomContext())
{
    using (TransactionScope trans = new TransactionScope())
    {
        SysLog updatlog = dbContext1.SysLogs.Find(1);
        updatlog.UserName = "ABCDAAA -- -- -- --";
        updatlog.Introduction += "---ABCDEFG";
        dbContext1.SaveChanges();
        SysLog updatlog2 = dbContext2.SysLogs.Find(1);
        updatlog2.UserName = "Test ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~...";
        updatlog2.Introduction += "---ABCDEFG";
        dbContext2.SaveChanges(); 
        trans.Complete();// The transaction is complete only after the normal execution}}#endregion
Copy the code

encapsulation

public class UnitOfWork
{
    public static void Invoke(Action action)
    {
        TransactionScope transaction = null;
        try
        {
            transaction = new TransactionScope();
            action.Invoke();
            transaction.Complete();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw; }}}Copy the code
UnitOfWork.Invoke(() =>
{
    using (IUserCompanyService iUserCompanyService = new UserCompanyService(new JDDbContext()))
   {
        / / to add authorization}});Copy the code