开发者

using stored procedures as functions from edmx file

i have an entity-model file (edmx) file which contains few tables and few stored procedu开发者_如何学JAVAres.

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.


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.


I ran into this and found that I was missing the "Function Import" step that is described here: http://weblogs.asp.net/dotnetstories/archive/2011/03/01/using-stored-procedures-with-entity-framework-in-an-asp-net-application.aspx

In a nutshell:

  1. Open your .edmx designer
  2. Right click and choose add->"Function Import" from menu.
  3. Select your stored procedure from the dropdown.
  4. Add the Name.
  5. Click ok.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜