nosbor
nosbor

Reputation: 2999

Database error handling in entity framework

I wrote stored procedure witch sometimes RAISERROR(). I execute it through the entity framework like:

using( MyModelEntities conn = new MyModelEntities() ) {
    conn.MyStoredProcedure(input_p, output_p);
}

Stored procedure:

create procedure dbo.MyStoredProcedure(
    @input   nvarchar(255),
    @output int out
)
as
begin
    ...
        RAISERROR (N'My Exception....', 10, 1);
    ...
end
go

Is there any opportunity to get information about error?

Upvotes: 6

Views: 3491

Answers (3)

Utkarsh Patel
Utkarsh Patel

Reputation: 325

In sql server exception level 1 to 10 are informational and does not raise error back to your application.

change Level in between 11 to 16 to raise error from the SP.

Upvotes: 4

CodeGrue
CodeGrue

Reputation: 5933

Here is how I handle this:

create procedure dbo.MyStoredProcedure(
    @input   nvarchar(255),
    @output  text out
)
as
begin
    ...
      SELECT @message = convert(varchar, getdate(), 108) + ': My Exception....'
      SET @output = CAST(@message AS text) -- send message to c#/.net
      RAISERROR(@message,0,1) WITH NOWAIT  -- send message to SQL Server Management Studio
    ...
end
go

Then on the C#/EF side:

public string CallMyStoredProcedure(string input)
{
    var outputParameter = new ObjectParameter("output", typeof(string));
    EntityContext.MyStoredProcedure(input, outputParameter);
    return (string)outputParameter.Value;
}

Upvotes: 0

dweiss
dweiss

Reputation: 832

Can this just be put into a try/catch with the exception displayed? Or maybe you could try going into debug mode?

Upvotes: 2

Related Questions