EFCore的事务和分布式事务的使用

在操作数据库的时候,事务提交时我们必须考虑的问题,下面针对EFCore的事务进行介绍:

1.EFCore自带默认事务SaveChanges

EFCore 的一个Context连接对应的一次SaveChanges就是一个事务处理,

我们可以在一个Context里操作多个表数据,

有对一个表进行修改,对另一个表进行新增

然后一次性调用SaveChanges;

如下代码:

///DbContext SaveChanges 事务提交  事务提交
            ///事务特点:要不都成功  要么都知道
            using (EFCoreContext context = new EFCoreContext())
            {
                SysLog log = context.SysLog.FirstOrDefault(l => l.Id == 1);
                context.SysLog.Remove(log);
                SysUserInfo sysUserInfo1 = context.SysUserInfo.FirstOrDefault(a => a.Id == 6);
                SysUserInfo sysUserInfo2 = context.Set<SysUserInfo>().Find(5);
                sysUserInfo1.Name += "-11";
                sysUserInfo2.Name += "-22";

                context.SysLog.Add(new SysLog()
                {
                    UserName = "测试日志",
                    Introduction = "描述一下"
                });

                context.SaveChanges();
            }

 

2.IDbContextTransaction同一数据库事务处理

//如果我需要把两个SaveChange 事务一下的?
                using (EFCoreContext context = new EFCoreContext())
                { 
                    IDbContextTransaction tans = null;
                    try
                    {
                        tans = context.Database.BeginTransaction(); //框架对事务的支持
                        context.SysLog.Add(new SysLog()
                        {
                            UserName = "第一次SaveChanges",
                            Introduction = "第一次SaveChanges",
                            CreateTime = DateTime.Now
                        });
                        context.SaveChanges();

                        context.SysLog.Add(new SysLog()
                        {
                            UserName = "第二次SaveChanges",
                            Introduction = "第二次SaveChanges",
                            CreateTime = DateTime.Now
                        });
                        context.SaveChanges();
                        tans.Commit();  //代码只有执行到这里事务才能生效
                    }
                    catch (Exception ex)
                    {
                        if (tans != null)
                        {
                            tans.Rollback();//事务回退
                        }
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        tans.Dispose();
                    }
                }

 

3.TransactionScope分布式事务

多个数据库之间的事务提交用TransactionScope,也就是多个Context的提交,下面模拟多个数据库的Context

如下代码:

 ///如果我需要把两个SaveChange 事务一下的?
                using (EFCoreMigrationContext context1 = new EFCoreMigrationContext())  //招商银行的数据库
                using (EFCoreMigrationContext context2 = new EFCoreMigrationContext())////中国银行的数据库
                {
                    //需要引入System.Transactions.Local.dll
                    using (TransactionScope transactionScope = new TransactionScope())
                    { 
                        try
                        { 
                            context1.SysLog.Add(new SysLog()
                            {
                                UserName = "context1新增一条测试数据",
                                Introduction = "context1新增一条测试数据",
                                CreateTime = DateTime.Now
                            });
                            context1.SaveChanges();

                            context2.SysLog.Add(new SysLog()
                            {
                                UserName = "context2新增一条测试数据",
                                Introduction = "context2新增一条测试数据",
                                CreateTime = DateTime.Now
                            });
                            context2.SaveChanges();
                            transactionScope.Complete();//提交事务

                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }

 

posted @ 2020-09-18 11:30  幻影星辰  阅读(2138)  评论(1编辑  收藏  举报