How can I use LINQ to Entities to invoke an Oracle function with a return value?

entity-framework linq-to-entities oracle11g


I'm working on an application which accesses data from an Oracle 11g database. I'm using EF4 and data is accessed using LINQ. I have come across a scenario where I need to call a function stored in a package. This function also has a return value. I have added this function to the entity data model but am unable to perform "Add Function Import" on it. Hence I'm unable to access it using LINQ. How can I call this function and also get it's return value?

I asked this question a while back, but have not got any answer for it yet. Anyways, I'm updating it with some details so that it becomes convenient for others to understand the issue and guide me in the right direction. I have tried to implement the solution proposed in this question, but am getting an exception.

I've added the following to the designer.cs file of my entity data model:

   [EdmFunction("TestModel.Store", "TestFunction")]
    public int TestFunction(decimal ALNR, decimal ATID, decimal AUKENR)
        throw new ApplicationException();

Following is a small portion of the edmx file:

<edmx:Edmx Version="2.0" xmlns:edmx="">
<!-- EF Runtime content -->
    <!-- SSDL content -->
           <Schema Namespace="TestModel.Store" Alias="Self" Provider="Oracle.DataAccess.Client" ProviderManifestToken="11g" xmlns:store="" xmlns="">
               <Function Name="TestFunction" ReturnType="number" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" StoreFunctionName="PKG_TURNUS.SUMUKE" Schema="SYSTEM">
      <Parameter Name="ATID" Type="number" Mode="In" />
      <Parameter Name="ALNR" Type="number" Mode="In" />
      <Parameter Name="AUKENR" Type="number" Mode="In" />

Here is how I am calling this function:

var selectQuery = from T in _context.Table1
                  join A in _context.Table2 on T.columnA equals A.columnB
                  join TU in _context.Table3 on T.columnC equals TU.columnD
                  where T.columnD == 5 && T.columnE == someVariable
                  select new someType
                      propertyA = A.columnG,
                      propertyB = _context.TestFunction(T.columnE, A.columnF, TU.columnH) 

But when I do any of the following:

ObservableCollection<someType> weekTotaldata = new ObservableCollection<someType>(selectQuery);  //Exception thrown at runtime


foreach (var ttu in selectQuery) //Exception thrown at runtime
    double testval = ttu.propertyB;

I get the exception that is thrown in that function "ApplicationException". Shouldn't this exception be thrown when this function is called anywhere else exception the L2E query?

What am I doing wrong? How can I call an oracle function from Linq-To-Entities? Please note that the function that I am trying to call is not a built-in oracle function, but a user-defined function.

5/23/2017 12:07:46 PM

Accepted Answer

As far as i have worked on EF4 using Oracle , Function importing doesn't seems to work here. I faced the same problem few months back and tried many ways to import a function but without any luck. But during searching I found a link on OTN which states that (Oracle Stored Functions are not supported). EF4 doesn't give us the option to call oracle function yet. Even using Stored Procedures, you will need to select stored procedures that return a ref cursor. Supported Stored procedures include procedures and package methods that do not have a return value, but may have OUT or IN OUT parameters.

Here is the link

But if you are using Sql server some how you can accomplish the User Defined Function import in EF4. Here are some links that might helps you:



6/23/2011 9:30:16 AM

Related Questions


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