(嵌套事务不支持)当数据插入使用Excel数据库表中的数据库

当我厌倦了将数据插入到使用entity framework的Excel表格中的数据时,我想在每个logging的循环内部处理exception,所以我在循环内使用了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); } } } } 

这里当Outstanding的对象是附件时,如果没有exception,应该插入到数据库中,如果没有显示exception,再次循环下一个事务并分离上一个logging对象。

这里我的问题是,当我插入3条logging没有任何exception工作正常,如果两条logging失败,最后第三条logging也不例外,它应该插入工作正常,但如果第一条logging插入,第二条logging失败,一些exception(重复键)比第三条logging也失败,下面的例外,但它应该插入((例外:不支持嵌套事务)

请尝试下面的链接它可能是由交易中使用的2个不同的连接引起的。 你应该手动控制连接:下面的解决scheme链接可能对你有帮助

在Entity Framework 4.0中“不支持嵌套事务”显示错误?