How to utilize Entity Framework with POCOs and Stored Procedures

c# entity-framework poco stored-procedures

Question

In this case, I'm attempting to utilize Entity Framework with POCOS, Stored Procedures, and 2 distinct projects.

I have two projects: one called BusinessEntities and one called DataAccess (for my edmx and DataContexts) (for my POCOs).
BusinessEntities are referenced in DataAccess.

I have a rather common SP in my database:

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

This is the data context:

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

If I merely do this (change the EDMX with the SP but do not import the function), I get the following error:

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

If I successfully import the method, I get the following error:

System. The type argument "BusinessEntites" caused an invalid operation exception. The type "DbModel" and GetHeaderResult in ExecuteFunction are incompatible. The function's GetHeaderResult return value is.

I suppose that all this is lacking is a straightforward setting, but I can't seem to find it.

Please note that the EDMX file is configured properly (CodeGenerationStrategy set to none, CustomTool is empty)

1
1
11/30/2011 8:31:49 PM

Accepted Answer

You are calling the incorrect method on the context in the first scenario.ExecuteFunction only applies to function imports. UseExecuteStoreQuery and SqlParameter instead. In the second scenario, calling a function import also generates a complex type in your EDMX, and EF anticipates that you'll utilize that complicated type as a consequence.

6
12/1/2011 9:19:16 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow