Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Oracle function, with return value using LINQ to Entities?

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="http://schemas.microsoft.com/ado/2008/10/edmx">
<!-- EF Runtime content -->
    <edmx:Runtime>
    <!-- SSDL content -->
        <edmx:StorageModels>
           <Schema Namespace="TestModel.Store" Alias="Self" Provider="Oracle.DataAccess.Client" ProviderManifestToken="11g" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
               <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" />
    </Function>

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

or

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.

like image 665
Hasan Fahim Avatar asked Oct 12 '22 00:10

Hasan Fahim


1 Answers

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:

Link1

Link2

like image 160
Syeda Avatar answered Jan 01 '23 10:01

Syeda