我已尝试使用以下代码进行实体事务,但不幸的是它 不适用于事务回滚。请帮帮我。
public void AddAGMDate()
{
DbTransaction Transaction = null;
using (EntityConnection DatabaseConnection = new EntityConnection("name=Insta_SAEntities"))
{
try
{
if (DatabaseConnection.State == ConnectionState.Closed)
DatabaseConnection.Open();//open connection
var CurrentContext = new Insta_SAEntities(DatabaseConnection);
Transaction = CurrentContext.Connection.BeginTransaction();//begin transaction
TR_AGMDatesEntityObject objAGMDates = new TR_AGMDatesEntityObject();
objAGMDates.ClientID = Convert.ToInt64(ddlClientName.SelectedValue);
objAGMDates.AGMDate = Convert.ToDateTime(txtAGMDate.Text.Trim());
objAGMDates.CreatedBy = 0;
objAGMDates.CreatedDate = Convert.ToDateTime(System.DateTime.Now);
objAGMDates.UpdatedBy = 0;
objAGMDates.UpdatedDate = Convert.ToDateTime(System.DateTime.Now);
int result = AGMDatesBL.AddAGMDate(objAGMDates);
if (true)
{
int n1 = 10, n2 = 0;
int res = n1 / n2;//Error, want transaction to rollback (for taesting)
}
Transaction.Commit();//commit transaction
if (result == 1)
{
ScriptManager.RegisterClientScriptBlock(Page, GetType(), "script", "alert('AGM Date Successfully entered ');" +
"window.location.href='ManageAGMDate.aspx';", true);
}
else if (result == 2)
{
txtAGMDate.Text = "";
ScriptManager.RegisterClientScriptBlock(Page, GetType(), "script", "alert('AGM Date Already Exist!');", true);
}
else if (result == 0)
{
ScriptManager.RegisterClientScriptBlock(Page, GetType(), "script", "alert('Unable to Add AGM Date!');" +
"window.location.href='ManageAGMDate.aspx';", true);
}
}
catch (Exception ex)
{
Transaction.Rollback();//rollback transaction
if (log.IsErrorEnabled) log.Error(string.Format("{0} ,Page URL={1}, Method Name={2} ,Error={3}", _LogMsgAdd, Request.Url.AbsoluteUri, MethodInfo.GetCurrentMethod().DeclaringType.FullName + "." + MethodInfo.GetCurrentMethod().Name, ex.Message.ToString()));
Session["error"] = ex.Message.ToString();
Response.Redirect("~/Common/Errors/ErrorPageWM.aspx?ShB=Y", false);
}
finally
{
if (DatabaseConnection != null)
{
if (DatabaseConnection.State == ConnectionState.Open)
DatabaseConnection.Close();
}
}
}
答案 0 :(得分:2)
您是否尝试使用System.Transactions.TransactionScope **? (这是在.NET中处理事务的流行和常用方法。)
例如
using (EntityConnection DatabaseConnection =
new EntityConnection("name=Insta_SAEntities"))
{
using(var transactionScope = new TransactionScope())
{
TR_AGMDatesEntityObject objAGMDates = new TR_AGMDatesEntityObject();
// Set properties...
int result = AGMDatesBL.AddAGMDate(objAGMDates);
// I have no idea what this logic does ... but I don't care, cause i'm just
// copying what u wrote.
if (true)
{
int n1 = 10, n2 = 0;
int res = n1 / n2;//Error, want transaction to rollback (for taesting)
}
else
{
transactionScope.Commit();
}
}
// The rest of your logic now happens here....
好的..所以如果你不在Commit()
区块内做TransactionScope()
,那么它会自动执行RollBack()
;否则,您可以使用transactionScope.RollBack();
强制回滚..比如
if (true)
{
int n1 = 10, n2 = 0;
int res = n1 / n2;
transactionScope.RollBack();
}
else
{
transactionScope.Commit();
}
HTH。
****青少年玩家的陷阱**:此交易内容代码位于System.Transactions.dll
..所以请确保添加该程序集。