简体   繁体   English

使用EF失败调用oracle存储过程

[英]Call oracle Stored Procedure with EF fail

I have an oracle store procedure which take 2 strings and a date in input parametter and which give a ref cursor as output : 我有一个oracle存储过程,它在输入参数中包含2个字符串和一个日期,并将ref光标作为输出:

CREATE OR REPLACE PROCEDURE SCHEMA.MYPROSTO (
   pPl    IN     VARCHAR2, -- Comma (;) separated
   pTy    IN     VARCHAR2,-- Comma (;) separated
   pDate     IN     mytable.mydate%TYPE,
   pCursor      OUT sys_refcursor)
IS
   .....
   sSQL      VARCHAR2 (3000);
BEGIN

   -- making SQL Order
   sSQL := 'SELECT TO_CHAR (v.date_c........

   ......


   OPEN pCursor FOR sSQL;

END MYPROSTO;

The output cursor return a set of 3 string cells rows. 输出游标返回一组3个字符串单元格行。

I imported this stored procedure in my entity framework model, with this in the .config file : 我在我的实体框架模型中导入了这个存储过程,在.config文件中:

<oracle.manageddataaccess.client>
<version number="*">
  <implicitRefCursor>
    <storedProcedure schema="SCHEMA" name="MYPROSTO">
      <refCursor name="PCURSOR">
        <bindInfo mode="Output"/>
        <metadata columnOrdinal="0" columnName="YEAR" providerType="Varchar2" allowDBNull="true" nativeDataType="Varchar2"/>
        <metadata columnOrdinal="1" columnName="MONTH" providerType="Varchar2" allowDBNull="true" nativeDataType="Varchar2"/>
        <metadata columnOrdinal="2" columnName="COUNT" providerType="Varchar2" allowDBNull="true" nativeDataType="Varchar2"/>
      </refCursor>
    </storedProcedure>
  </implicitRefCursor>
</version>
</oracle.manageddataaccess.client>

The function import wizzard created a result object and generated an access function : 函数import wizzard创建了一个结果对象并生成了一个访问函数:

public virtual ObjectResult<MYPROSTO_Result> MYPROSTO (string pPL, string pTY, Nullable<System.DateTime> pDATE)
{
    var pPLParameter = pPL!= null ?
        new ObjectParameter("PPL", pPL) :
        new ObjectParameter("PPL", typeof(string));

    var pTYParameter = pTY!= null ?
        new ObjectParameter("PTY", pTY) :
        new ObjectParameter("PTY", typeof(string));

    var pDATEParameter = pDATE.HasValue ?
        new ObjectParameter("PDATE", pDATE) :
        new ObjectParameter("PDATE", typeof(System.DateTime));

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<MYPROSTO_Result>("MYPROSTO", pPLParameter, pTYParameter, pDATEParameter);
}

However, the call to this function raise an exception ( System.Data.Entity.Core.EntityCommandExecutionException ) on the last line : 但是,对此函数的调用会在最后一行引发异常( System.Data.Entity.Core.EntityCommandExecutionException ):

ORA-06550: Ligne 1, colonne 8 : PLS-00306: wrong number or types of arguments in call to 'MYPROSTO'
ORA-06550: Ligne 1, colonne 8 : PL/SQL: Statement ignored

I don't see why it fail 我不明白为什么会失败

There appears to be several problems: 似乎有几个问题:

  • Sending a varchar(2) into a date field 将varchar(2)发送到日期字段
  • The name of a parameter "pTY" versus "pType" 参数“pTY”与“pType”的名称
  • The name of a parameter "pPL" versus "pPlant" 参数“pPL”与“pPlant”的名称
  • The name of a parameter "PPLT" versus "PPL" 参数“PPLT”与“PPL”的名称

What about return parameter. 返回参数怎么样? It is "mapped" ok? 它被“映射”了吗? DoubleCheck 再检查一遍

Surely the answer is clear in the error message: 肯定在错误消息中明确答案:

"wrong number or types of arguments in call to 'MYPROSTO'" “调用'MYPROSTO'时参数的数量或类型错误”

Your procedure expects 4 parameters but you're only passing 3. You need an output variable for the refcursor. 您的过程需要4个参数,但您只需要传递3.您需要一个refcursor的输出变量。

I had the same thing happen to me and FINALLY solved it. 我有同样的事情发生在我身上,最终解决了它。 I was using the oracle.manageddataaccess.client, like you, and had my .NET solution divided into a Presentation Project, WebAPI Project, and Data Access Project. 我和你一样使用oracle.manageddataaccess.client,并将我的.NET解决方案分为Presentation Project,WebAPI Project和Data Access Project。 My app.config in the Data Access Project had the correct ImplicitRefCursor section with the cursor definition and metadata like you have, but I forgot to also copy it into my web.config of my WebAPI project. 我在数据访问项目中的app.config具有正确的ImplicitRefCursor部分,其中包含光标定义和元数据,但我忘了将其复制到我的WebAPI项目的web.config中。 Once I did that, my "wrong number or types of arguments" error went away. 一旦我这样做,我的“错误数量或类型的参数”错误就消失了。 Hope that helps. 希望有所帮助。 (I just copied the whole oracle.manageddataaccess.client section.) (我刚刚复制了整个oracle.manageddataaccess.client部分。)

Also, if you generated that whole ImplicitRefCursor section by hand, I discovered a much easier and reliable way to do that. 此外,如果您手动生成整个ImplicitRefCursor部分,我发现了一种更简单可靠的方法。 From the .NET Server Explorer, connect to your database, find your stored procedure, right-click on it and RUN. 从.NET Server Explorer,连接到您的数据库,找到您的存储过程,右键单击它并运行。 Then OK. 那好吧 It will bring up a list of IN and OUT parameters, including the cursor. 它将显示IN和OUT参数列表,包括光标。 If you click on Show Config button, it will show you what needs to be in your EF app.config. 如果单击“显示配置”按钮,它将显示您的EF app.config中需要的内容。 And the AddConfig button will add it for you. AddConfig按钮将为您添加它。 That helps to avoid mistakes. 这有助于避免错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM