I have the following code in my application:
using (var database = new Database()) {
var poll = // Some database query code.
foreach (Question question
You should wrap the contents of your using block in a try/catch and roll back the transaction in the catch block:
using (var database = new Database()) try
{
var poll = // Some database query code.
foreach (Question question in poll.Questions) {
foreach (Answer answer in question.Answers) {
database.Remove(answer);
}
// This is a sample line that simulate an error.
throw new Exception("deu pau");
database.Remove(question);
}
database.Remove(poll);
}
catch( /*...Expected exception type here */ )
{
database.Rollback();
}