Suncat2000
Suncat2000

Reputation: 1086

Entity Framework error "Could not find stored procedure ''"

Our C# application, using Entity Framework 6.4.4 to access an SQL Server instance, intermittently fails to return data from a stored procedure and throws n exception

EntityCommandExecutionException: Could not find stored procedure ' '

This has happened on a customer's production server about a dozen times over the past several months. We tried to troubleshoot this problem by examining SQL Server logs and exception details that we capture into Windows event logs. We attempted on multiple occasions to reproduce the problem under controlled conditions on development and staging servers, but could not successfully duplicate it.

Scouring the Web for information, we have never found any documentation about this particular problem. The closest we ever found was a bug report in the Entity Framework 6 project on GitHub when implementing IDbInterceptor. We implemented an interceptor on a single query; however, we encountered this error on other queries that didn't use the interceptor.

The kind of error behaves suspiciously like a resource exhaustion problem. However, this is not supported by examination of application and server logs that do not show any unusually high resource usage when these errors occur.

The code that calls the stored procedure is generated from an EDMX file (database-first model) by the Entity Model Code Generator in Visual Studio. Examples of the stored procedure method, call, and data type are as follows; all our stored procedure calls follow the same pattern. A stack trace from the captured exception points directly to one of these calls. None of the stored procedure methods ever omit the name and they work under normal circumstances.

Example entity type GetAllConditionCodes_Result:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace DAL.Entities
{
    using System;
    
    public partial class GetAllConditionCodes_Result
    {
        public int ID { get; set; }
        public string ConditionCodeNumber { get; set; }
        public string Description { get; set; }
        public bool IsDeleted { get; set; }
    }
}

EF method that calls a stored procedure GetAllConditionCodes, named by a string literal in the ExecuteFunction call:

public virtual ObjectResult<GetAllConditionCodes_Result> GetAllConditionCodes(Nullable<System.DateTime> lastUpdateTime)
{
    var lastUpdateTimeParameter = lastUpdateTime.HasValue ?
            new ObjectParameter("LastUpdateTime", lastUpdateTime) :
            new ObjectParameter("LastUpdateTime", typeof(System.DateTime));

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetAllConditionCodes_Result>("GetAllConditionCodes", lastUpdateTimeParameter);
}

Application method, in our data access layer, that requests the stored procedure call. The Mapper.Map statement just shows our use of AutoMapper to convert entities (GetAlLConditionCodes_Result) into data model (ConditionCode) objects. The CreateObjectContext call returns the EF DbContext object that provides entity access to the database.

public List<ConditionCode> GetAllConditionCodesFromDatabase(DateTime? lastUpdateTime = null)
{
    var conditionCodes = new List<ConditionCode>();

    using (var context = CreateObjectContext())
    {
        var result = context.GetAllConditionCodes(lastUpdateTime).ToList();
        conditionCodes = Mapper.Map<List<Entities.GetAllConditionCodes_Result>, List<ConditionCode>>(result);
    }

    return conditionCodes;
}

The outer exception

System.Data.Entity.Core.EntityCommandExecutionException

that our application captures contains the message:

An error occurred while executing the command definition. See the inner exception for details.

The inner exception (type System.Data.SqlClient.SqlException) contains the (literal) message:

Could not find stored procedure ' '

Note that the reported name is a string containing a single space character instead of the name of the stored procedure that should have been called.

Any help in leading to a resolution of this problem would be much appreciated.

Upvotes: 0

Views: 204

Answers (0)

Related Questions