Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework database first does not catches sql Raiserror at the end of a stored procedure
    text
    copied!<p>I've been googling around and I have found similar questions but not exactly this case. I have a sql server 2005 stored procedure which raises an error at the end, after any data processing call.</p> <pre><code> Create Procedure webzTest( @nClaPais Int ) As Begin Select * From zPais Where ClaPais = @nClaPais Raiserror('TEST ERROR!!!', 16, -1) End </code></pre> <p>On the client side Entity Framework (Visual Studio 2012) tries to catch the exception, but it ignores it.</p> <pre><code> public static void webzTestTransaction() { using (var ctx = new MyWebEntities()) { using (var trx = MyTransactionScope.newTransactionScope()) { var connection = ((IObjectContextAdapter)ctx).ObjectContext.Connection; connection.Open(); // Open connection explicitly to avoid EF closing and reopening which invokes DTC try { ctx.webzTest(1).ToList(); ctx.webzTest(2).ToList(); trx.Complete(); } catch (Exception ex) { logger.Error(ex); throw ex; } finally { connection.Close(); } } } } </code></pre> <p>The raised error is ignored. If I put the Raiserror() statement BEFORE the Select then the exception is catched by EF code, but I expect the same behaviour in the above code.</p> <p>Let me add a similar example using ADO.NET. In ths case, the exception is catched as expected:</p> <pre><code> SqlConnection conn = new SqlConnection(connectionString); conn.Open(); SqlTransaction trx = null; try { trx = conn.BeginTransaction(IsolationLevel.ReadUncommitted); SqlCommand cmd = new SqlCommand(); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Connection = conn; cmd.CommandText = "webzTest"; cmd.Transaction = trx; var parClaPais = new SqlParameter("@nClaPais", SqlDbType.Int); cmd.Parameters.Add(parClaPais); parClaPais.Value = 1; SqlDataReader result1 = cmd.ExecuteReader(); parClaPais.Value = 2; SqlDataReader result2 = cmd.ExecuteReader(); trx.Commit(); trx = null; } catch (Exception ex) { if (trx != null) trx.Rollback(); throw ex; } finally { conn.Close(); } </code></pre> <p>Then: What is wrong with EF? or Which is the rule to follow in this case?</p> <p>Thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload