When we execute the stored procedure using entity framework sometimes we got error
The wait operation timed out
. In this post I want to explain how to solve this issue. Most probably of this issue is the stored procedure take more time (more than 30 sec). For demonstration purpose I was created a stored procedure like bellow.
CREATE PROCEDURE
LongRunTimeProcedureAS
BEGIN
--Your implementation
--Your demo purpose I use WAITFOR DELAY Class
WAITFOR DELAY
'00:01'
Select
* from
CountryMaster END
GO
When we execute the above stored procedure from SSMS it take 1 mints. See the bellow image.
WAITFOR DELAY
'00:01'
query stop the execution for I mints after I 1 mints it will execute the next Select * from CountryMaster
If we run the above the stored procedure using Entity framework model first concept it will throw the Timeout error like bellow.
Because the default CommandTimeout of entity framework is 30 seconds. But stored procedure estimate time is 1 min. now see how to solve the problem.
To solve this problem just change the CommandTimeout to 0 before to call the stored procedure like bellow.
public virtual
ObjectResult< longruntimeprocedure_result
> LongRunTimeProcedure(){
((IObjectContextAdapter)
this).ObjectContext.CommandTimeout = 0;
return ((IObjectContextAdapter)this
<longruntimeprocedure_result
>("LongRunTimeProcedure"
}
This is immediate solution only not an permanent solution, for permanent solution you should increase the performance of stored procedure. Please see the link how to improve how to improve the performance of stored procedure.
No comments:
Post a Comment