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

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

1.EFCore自带默认事务SaveChanges框架

EFCore 的一个Context链接对应的一次SaveChanges就是一个事务处理,分布式

咱们能够在一个Context里操做多个表数据,测试

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

而后一次性调用SaveChanges;日志

以下代码:code

///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同一数据库事务处理blog

//若是我须要把两个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的提交,下面模拟多个数据库的Contextit

以下代码:

 ///若是我须要把两个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);
                        }
                    }
                }
相关文章
相关标签/搜索