views:

772

answers:

2

hi,

i have an entity-model file (edmx) file which contains few tables and few stored procedures.

how can i call those stored procedures that are mapped to functions? i thought it should be trivial, and i do see the mapping in the edmx file, but i can't figure how to use it in the code.

here is one mapping example:

       <Function Name="pa_crud_broker_ADD" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
          <Parameter Name="BrokerId" Type="int" Mode="InOut" />
          <Parameter Name="Name" Type="nvarchar" Mode="In" />
          <Parameter Name="Identifier" Type="nvarchar" Mode="In" />
        </Function>

          <FunctionImport Name="pa_crud_broker_ADD" ReturnType="Collection(Int32)">
            <Parameter Name="BrokerId" Mode="InOut" Type="Int32" />
            <Parameter Name="Name" Mode="In" Type="String" />
            <Parameter Name="Identifier" Mode="In" Type="String" /></FunctionImport>
<FunctionImportMapping FunctionImportName="pa_crud_broker_ADD" FunctionName="PAEntities.store.pa_crud_broker_ADD" />

i would appreciate any help.

Thanks.

A: 

I was under the impression that you can't call stored procs...Entity Framework calls them for you...basically stored procs are an optional substitute for real time SQL generation and Entity Framework calls them as needed.

I haven't heard of being able to "call" stored procs manually in Entity Framework...

MrLane
You can definitely call stored procedures from within Entity Framework. http://msdn.microsoft.com/en-us/library/bb896279.aspx
jrista
That link explains how to define stored procs in the model, but not how to actually call them.
MrLane
Click the links at the bottom. Your answer is wrong.
Craig Stuntz
+2  A: 

I am a little rusty, however you should be able to call your function in one of two ways. If you are generating an ObjectContext from your model, then you should have a method on your object context that matches the name of your Function (in your case, pa_crud_broker_ADD). You should be able to call it like so:

var objectContext = new MyObjectContext(...);
var result = objectContext.pa_crud_broker_ADD(...);

If you are not generating an ObjectContext from your model, then you should be able to use the following:

var objectContext = new ObjectContext(...);
var result = objectContext.ExecuteFunction<List<int>>("pa_crud_broker_ADD", ...);

I am not entirely certain about the return result in the second case. I am not sure if EF v1 supports such a transformation or not. I know that EF v4 adds some considerable improvements in this area, so if EF v1 does not support it, I would look into EF v4.

jrista