EF事务的使用

EF事务的使用

一、SaveChanges自带事务

///1. Context 包含的能对数据库的操作肯定不是对一个表的操作;可以操作多个动作 
///2. SaveChanges以后,默认启动事务,会一次性提交对数据库的所有操作; 要么都成功  要么都失败
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 = "测试一下公司0001"
        };
        context.Companies.Add(adCompany001);
        Company adCompany002 = new Company()
        {
            Name = "测试一下公司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();
}

二、EF执行sql 事务

//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();//只有在BeginTransaction  Commit 的时候才会提交事务;
        }
        catch (Exception ex)
        {
            if (trans != null)
                trans.Rollback();
            throw ex;
        }
        finally
        {
            trans.Dispose();
        }
    }
}

三、分布式事务

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 = "测试一下~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·";
        updatlog2.Introduction += "---ABCDEFG";
        dbContext2.SaveChanges(); 
        trans.Complete();// 事务:  只有正常执行完这句话 才能算全部完整
    }
    
}

四、封装

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;
        }
    }
}
UnitOfWork.Invoke(() =>
{
    using (IUserCompanyService iUserCompanyService = new UserCompanyService(new JDDbContext()))
   {
        //增删改
   }
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

武功山上捡瓶子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值