/// <summary>
/// 使用事务TransactionScope
/// </summary>
static void UsingTransactionScope()
{
string value1Ofkey1 =
System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
using (var conn = new SqlConnection(value1Ofkey1))
{
var Id = new System.Data.SqlClient.SqlParameter("@Id",
System.Data.SqlDbType.Int, 0,
System.Data.ParameterDirection.Input,
0, 0, "Id",
System.Data.DataRowVersion.Current, false, null,
"", "", "");
Id.Value = 1;
conn.Open();
var sqlCommand = new SqlCommand();
sqlCommand.Connection = conn;
sqlCommand.CommandText =
@"UPDATE Employee SET Name = 'helenhelen222'" +
" WHERE ID=@ID";
sqlCommand.Parameters.Add(Id);
sqlCommand.ExecuteNonQuery();
using (var context = new EntFraContext(conn,
contextOwnsConnection: false))
{
var query = context.AccountSet.Where(p => p.Employee.Id == (int)Id.Value);
foreach (var item in query)
{
item.UserName += "卡卡卡";
}
context.SaveChanges();
}
}
scope.Complete();
}
}