using (var srop=DbHelper.CurrentDb.GetTransaction())
{
ID = bp.AddModel(model).ToStr();
#region 参与楼盘信息
if (ID.ToInt32() > 0)
{
FY_ActivityProjectServer dtlServer = new FY_ActivityProjectServer();
for (int i = 0; i <= this.Projects.Items.Count; i++)
{
if (this.Projects.Items[i].Selected)
{
FY_ActivityProject dtl = new FY_ActivityProject();
dtl.ActivityID = ID.ToInt32();
dtl.ProjectID = this.Projects.Items[i].Value.ToInt32();
dtl.IsDel = false;
dtlList.Add(dtl);
}
}
dtlServer.AddModel(dtlList);
}
#endregion
srop.Complete();
}

以上是第一种写法

下面是第二种写法:

DbHelper.CurrentDb.BeginTransaction();//事务开始
this.ModelDataCollect();
FY_ActivityServer bp = new FY_ActivityServer();
try
{
ID = bp.AddModel(model).ToStr();
#region 参与楼盘信息
if (ID.ToInt32() > 0)
{
FY_ActivityProjectServer dtlServer = new FY_ActivityProjectServer();
for (int i = 0; i <= this.Projects.Items.Count; i++)
{
if (this.Projects.Items[i].Selected)
{
FY_ActivityProject dtl = new FY_ActivityProject();
dtl.ActivityID = ID.ToInt32();
dtl.ProjectID = this.Projects.Items[i].Value.ToInt32();
dtl.IsDel = false;
dtlList.Add(dtl);
}
}
dtlServer.AddModel(dtlList);
}
#endregion
DbHelper.CurrentDb.CompleteTransaction();
}
catch (Exception)
{
DbHelper.CurrentDb.AbortTransaction();
}

以上操作有两个insert操作

第一个insert能插入成功,第二个insert方法故意让它报错,

问题来了,

PetaPoco 不会回滚,导致第一个操作插入成功,第二个操作插入失败

求解决方案

04-29 23:58