C# (不支持嵌套事务)在mysql数据库中使用excel工作表插入数据时

C# (不支持嵌套事务)在mysql数据库中使用excel工作表插入数据时,c#,mysql,excel,entity-framework-6,C#,Mysql,Excel,Entity Framework 6,当我厌倦了使用实体框架使用excel表将数据插入MySQL时, 这里,我想在循环中为每个记录处理异常,所以,我在循环中使用了try--catch enter code here foreach (DataRow dr in ds.Tables[0].Rows) { outstanding_master master = new outstanding_master(); {

当我厌倦了使用实体框架使用excel表将数据插入MySQL时, 这里,我想在循环中为每个记录处理异常,所以,我在循环中使用了try--catch

enter code here

        foreach (DataRow dr in ds.Tables[0].Rows)
            {
                 outstanding_master master = new outstanding_master();
                    {
                        //log.Debug("This a test debug message");

                        master.LoanID = dr["LoanID"].ToString();
                        master.Name = dr["Name"].ToString();
                        master.PhoneNumber = Convert.ToInt64(dr["PhoneNumber"].ToString());
                        master.Address = dr["Address"].ToString();
                        master.ServiceProviderID = Convert.ToInt32(dr
                         ["ServiceProviderID"].ToString());
                        master.TotalDue = dr["TotalDue"].ToString();
                        master.LastPaidAmount = dr["LastPaidAmount"].ToString();
                        master.LastPaidDate = Convert.ToDateTime(dr["LastPaidDate"].ToString());
                        master.OutStandingDescription = dr["OutStandingDescription"].ToString();
                        master.longitude = dr["longitude"].ToString();
                        master.latitude = dr["latitude"].ToString();

                        db.outstanding_master.Add(master);


                        try
                        {
                            db.SaveChanges();
                            log.Info("Record saved successfully with LoanID: " + master.LoanID);

                         }
                         catch (Exception ex)
                         {
                          log.Info("Error saving record with LoanID: " + dr["LoanID"].ToString() + " Exception(): " + ex.InnerException.InnerException);
                          var entry = db.Entry(master);
                          if (entry.State == EntityState.Added)
                          {
                              ((System.Data.Entity.Infrastructure.IObjectContextAdapter)db).ObjectContext.DeleteObject(master);

                          }
                     }  }  }
在这里,当未完成的对象是attachment if no exception时,它应该插入数据库if not show exception并再次循环以进行下一个事务,并分离上一个记录对象

这里我的问题是当我插入3条记录时,没有任何异常工作正常, 如果两条记录失败,最后第三条记录也不例外,则应插入正常工作 但如果插入第一条记录,第二条记录因某些异常(重复键)而失败,而第三条记录也因以下异常而失败,但应插入((异常:不支持嵌套事务)

请尝试下面的链接 这可能是由于事务中使用了两个不同的连接。您应该手动控制连接: 下面的解决方案表单链接可能对您有所帮助


为什么这个问题要结束?