entity framework 4 POCO's stored procedure err

2020-02-14 07:00发布

问题:

Entity Framework with POCO Entities generated by T4 template. Added Function Import named it "procFindNumber" specified complex collection named it "NumberResult".

Here's what got generated in Context.cs file:

public ObjectResult<NumberResult> procFindNumber(string lookupvalue)
{
   ObjectParameter lookupvalueParameter;

   if (lookupvalue != null)
   {
      lookupvalueParameter = new ObjectParameter("lookupvalue", lookupvalue);
   }
   else
   {
       lookupvalueParameter = new ObjectParameter("lookupvalue", typeof(string));
   }
   return base.ExecuteFunction<NumberResult>("procFindNumber", lookupvalueParameter);
}

Here's the stored procedure:

ALTER PROCEDURE [dbo].[procFindNumber] 
@lookupvalue varchar(255)   
AS
BEGIN
SET NOCOUNT ON;    
DECLARE @sql nvarchar(MAX); 

IF @lookupvalue IS NOT NULL AND @lookupvalue <> ''
    BEGIN                   
        SELECT @sql = 'SELECT dbo.HBM_CLIENT.CLIENT_CODE, dbo.HBM_MATTER.MATTER_NAME, dbo.HBM_MATTER.CLIENT_MAT_NAME 
                FROM dbo.HBM_MATTER INNER JOIN dbo.HBM_CLIENT ON dbo.HBM_MATTER.CLIENT_CODE = dbo.HBM_CLIENT.CLIENT_CODE 
                LEFT OUTER JOIN dbo.HBL_CLNT_CAT ON dbo.HBM_CLIENT.CLNT_CAT_CODE = dbo.HBL_CLNT_CAT.CLNT_CAT_CODE 
                LEFT OUTER JOIN dbo.HBL_CLNT_TYPE ON dbo.HBM_CLIENT.CLNT_TYPE_CODE = dbo.HBL_CLNT_TYPE.CLNT_TYPE_CODE 
                WHERE (LTRIM(RTRIM(dbo.HBM_MATTER.CLIENT_CODE)) <> '''')'
        SELECT @sql = @sql + ' AND (dbo.HBM_MATTER.MATTER_NAME like ''%' + @lookupvalue + '%'')'
        SELECT @sql = @sql + ' OR (dbo.HBM_MATTER.CLIENT_MAT_NAME like ''%' + @lookupvalue + '%'')'
        SELECT @sql = @sql + ' ORDER BY dbo.HBM_MATTER.MATTER_NAME'
        -- Execute the SQL query
        EXEC sp_executesql @sql
    END 
END

In my WCF service I try to execute the stored procedure:

[WebGet(UriTemplate = "number/{value}/?format={format}")]        
public IEnumerable<NumberResult> GetNumber(string value, string format)
{
   if (string.Equals("json", format, StringComparison.OrdinalIgnoreCase))
   {
       WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
   }

   using (var ctx = new MyEntities())
   {                
       ctx.ContextOptions.ProxyCreationEnabled = false;
       var results = ctx.procFindNumber(value);
       return results.ToList();
   }
}

Error message says "The FunctionImport ... could not be found in the container ..."

What am I doing wrong?

回答1:

You need to qualify the function import with the container name.

E.g change this:

return base.ExecuteFunction<NumberResult>("procFindNumber", lookupvalueParameter);

to this:

return base.ExecuteFunction<NumberResult>("EntityContainerName.procFindNumber", lookupvalueParameter);

The entity container name is found on your EDMX - right click anywhere and do "Properties".



回答2:

I had a similar problem with EF 4.1... and found that I would get this issue when the code was running in Release mode. In Debug mode, it works fine.