1

I am trying here to use Entity Framework with Stored procedures and POCOS and 2 different projects.

I have one project DataAccess (for my edmx and DataContexts) and one project BusinessEntities (for my POCOs).
DataAccess have a reference of BusinessEntities.

In my DB I have a pretty standard SP :

CREATE STORED PROCEDURE GetHeader
    @id CHAR(35)
AS
BEGIN
    SELECT ID, Name FROM mytable WHERE ID = @id
END

The datacontext is :

public class DbContext : ObjectContext
{
public ObjectResult<BusinessEntities.GetHeaderResult> GetHeader(string id)
{
return base.ExecuteFunction<BusinessEntities.GetHeaderResult>("GetHeader", new ObjectParameter("id", id));
}
}

If I only go like this (the EDMX has been updated with the SP but the function has not been imported) I have this error :

System.InvalidOperationException: The FunctionImport &#39;GetHeader&#39; could not be found in the container &#39;DbEntities&#39;.

If I import the function correctly I have this error :

System.InvalidOperationException: The type parameter 'BusinessEntites.GetHeaderResult' in ExecuteFunction is incompatible with the type 'DbModel.GetHeaderResult' returned by the function.

I guess that it is only just a simple setting that is missing here but I can't seem to grab it.

Please not that the EDMX file has the correct setting (CodeGenerationStrategy set to none, CustomTool is empty)

Erick
  • 5,969
  • 10
  • 42
  • 61

1 Answers1

6

In the first case you are calling wrong method on the context. ExecuteFunction is only for function imports. Use ExecuteStoreQuery and SqlParameter instead. In the second case function import also creates a complex type in your EDMX and EF expects you will use that complex type as a result of function import call.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • So basically if my BE are in another project, I need to use ExecuteStoreQuery and there is no other choices ? – Erick Dec 01 '11 at 14:19
  • 1
    I've posted an example here (http://stackoverflow.com/questions/8462161/entity-framework-ef4-1-stored-procedure-could-not-be-found-in-the-container/10654834#10654834) on this subject. – Kwex May 18 '12 at 14:58
  • " In the second case function import also creates a complex type in your EDMX" If i import two SProc's each with a different result set, will it create a complex type for each? Ive had a look through my partial class which inherits from ObjectContext, but cant find any other types in there and i selected the required SProc when i initially selected the table during the Entity data model wizard. Any ideas? Thanks – Hans Rudel Jan 10 '13 at 10:30
  • 1
    @Hans: Selecting stored procedures during EDM wizard is only first step. You then need to declare function import where complex type is created. – Ladislav Mrnka Jan 10 '13 at 12:19
  • Done, a new complex type is now showing :). Would it be possible to speak to u in a chatroom as im still having some issues regarding how to execute/store the results? Thanks for ur time. – Hans Rudel Jan 10 '13 at 12:38
  • @Hans: Send me an invite if that possible or just post a link to chant room. – Ladislav Mrnka Jan 10 '13 at 12:45