实体框架中的数据库错误处理

时间:2021-02-03 22:37:47

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

我编写了存储过程witch()。我通过如下实体框架执行:

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?

有机会得到关于错误的信息吗?

3 个解决方案

#1


3  

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

在sql server异常级别1到10中是信息性的,不会将错误返回到应用程序。

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

在11到16之间改变水平,从SP中增加误差。

#2


2  

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

是否可以将其放入带有异常显示的try/catch中?或者你可以尝试进入调试模式?

#3


0  

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:

然后在c# /EF端:

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

#1


3  

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

在sql server异常级别1到10中是信息性的,不会将错误返回到应用程序。

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

在11到16之间改变水平,从SP中增加误差。

#2


2  

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

是否可以将其放入带有异常显示的try/catch中?或者你可以尝试进入调试模式?

#3


0  

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:

然后在c# /EF端:

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