I have two tables. I am updating those tables using entity framework. here is my code
public bool UpdateTables()
{
UpdateTable1();
UpdateTable2();
}
If any table update operation fails other should not be committed how do i achieve this in entity framework?
using (TransactionScope transaction = new TransactionScope())
{
bool success = false;
try
{
//your code here
UpdateTable1();
UpdateTable2();
transaction.Complete();
success = true;
}
catch (Exception ex)
{
// Handle errors and deadlocks here and retry if needed.
// Allow an UpdateException to pass through and
// retry, otherwise stop the execution.
if (ex.GetType() != typeof(UpdateException))
{
Console.WriteLine("An error occured. "
+ "The operation cannot be retried."
+ ex.Message);
break;
}
}
if (success)
context.AcceptAllChanges();
else
Console.WriteLine("The operation could not be completed");
// Dispose the object context.
context.Dispose();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With