简体   繁体   中英

How to call Oracle stored procedure which accepts RefCursor as out parameter using Entity Framework

I have an Oracle stored procedure which accepts multiple out parameters of type RefCursor (code below).

create or replace 
    PROCEDURE PROCEDURE1(
    lookupvalues out SYS_REFCURSOR,
    lookupvalues1 out SYS_REFCURSOR)
AS 
BEGIN
    open lookupvalues for
    select attribute3 from table1 where attribute3 is not null;
    open lookupvalues1 for 
    select attribute1 from table2 where attribute1 is not null;
END PROCEDURE1;

I just need to get the result of those out parameter as data set using c# and Entity Framework.

I am able to get the data but I don't think it's a decent approach.

I want to use Entity Framework for retrieving the data (if possible).

using (APWCContext dbcontext = new APWCContext())
{
    using (var cmd = dbcontext.Database.Connection.CreateCommand())
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "PROCEDURE1";

        var businessUnit = new OracleParameter("lookupvalues", OracleDbType.RefCursor, ParameterDirection.Output);
        var currency = new OracleParameter("lookupvalues1", OracleDbType.RefCursor, ParameterDirection.Output);
        cmd.Parameters.AddRange(new[] { businessUnit,currency});                              
        cmd.Connection.Open();                    
        var reader= cmd.ExecuteReader();
        DataTable budt = new DataTable();                   
        while (reader.Read())
        {
            budt.Load(reader);
        }                                        
        var data = businessUnit.Value;                                                     
        cmd.Connection.Close();
    }
    //var businessUnit = new OracleParameter("lookupvalues", OracleDbType.RefCursor, ParameterDirection.Output);
    var r = dbcontext.GOODWILL_HEADER_STG.FirstOrDefault();

    //var result = dbcontext.Database.ExecuteSqlCommand("BEGIN PROCEDURE1(:lookupvalues); END;", businessUnit);
}

Without knowing the scope of your requirement fully, it looks like your procedure is not necessary. Alternative question could be why do you want to use Entity Framework?

Please see the answer referenced here:

Returning datatable using entity framework

You only need to define your entity table table1 once and then use Linq to extract the information.

See modified code extracted from linked answer:

DataTable dt = new DataTable();
(from a1 in context.Table1.Where(i => i.Attribute3 != "").AsEnumerable()
 from a2 in context.Table1.Where(i => i.Attribute1 != "").AsEnumerable()
   select new
          {
            Attribute = a2.Attribute1 ?? a1.Attribute3
            //etc
          }).Aggregate(table, (dt, r) =>
          {
            dt.Rows.Add(r.Attribute);
            return dt;
          });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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