Execute Oracle stored procedure in Entity Framework Core

.net-core c# entity-framework-core oracle sql-server

Question

When I tried to execute an Oracle stored procedure with Entity Framework Core, I get this exception :

No mapping to a relational type can be found for the CLR type 'OracleParameter[]

I tried ExecuteSqlCommand and FromSQL methods, both cause the same exception.

Oracle stored procedure code :

CREATE OR REPLACE Procedure GET_DEPT_PROC (
p_ParentSiteId number,
P_SiteId number,P_LoggedUserId number, P_curParam OUT sys_REFCURSOR) as
Begin
OPEN curParam FOR 
select 1 Id, 2 EntityAlmostOnRenewalTrans, 3 EntityVerifiedTrans, 4 
EntityCompletedTrans, 5 EntityClosedTrans, 6 MyVerifiedTrans, 7 
MyCreatedTrans, 8 MyDraftedTrans, 9 MyRejectedTrans FROM DUAL;

End;

C# method to execute this stored procedure:

public virtual DashBoardData GetDashBoardData(short parentSiteId, short siteId, int loggedUserId)
{
     var parameters = new OracleParameter[]
            {                        
                    new OracleParameter("@p_ParentSiteId", parentSiteId),
                    new OracleParameter("@p_SiteId", siteId),
                    new OracleParameter("@p_LoggedUserId", loggedUserId),                        
                    new OracleParameter("@p_curParam", OracleDbType.RefCursor, ParameterDirection.Output)};

     string sql = "EXEC GET_DEPT_PROC(@p_ParentSiteId,@p_SiteId,@p_LoggedUserId:@p_curParam);";

     var res = XDB.FromSql<DashBoardData>(base.context, sql, parameters).ToList()[0];
}

FromSQL Method in XDB class:

public static IQueryable<TEntity> FromSql<TEntity>(DbContext EFContext,RawSqlString sql, IEnumerable<OracleParameter> op) where TEntity : class
{
     return EFContext.Set<TEntity>().FromSql(sql, op);
}
1
1
8/22/2019 4:08:18 AM

Popular Answer

Here is the answer with example:

.: Oracle Stored Procedure:

create or replace Procedure GET_DEPT_PROC (
p_ParentSiteId number,
P_SiteId number,
P_LoggedUserId number, curParam OUT sys_REFCURSOR,curParam2 OUT sys_REFCURSOR )
as
Begin
OPEN curParam FOR 
select 1 Id, 2 EntityAlmostOnRenewalTrans, 3 EntityVerifiedTrans, 4 EntityCompletedTrans, 5 EntityClosedTrans, 6 MyVerifiedTrans, 7 MyCreatedTrans, 8 MyDraftedTrans, 9 MyRejectedTrans FROM DUAL;
OPEN curParam2 FOR
select 1 Id, 2 EntityAlmostOnRenewalTrans, 3 EntityVerifiedTrans, 4 EntityCompletedTrans, 5 EntityClosedTrans, 6 MyVerifiedTrans, 7 MyCreatedTrans, 8 MyDraftedTrans, 9 MyRejectedTrans FROM DUAL;
End; 

.: Execute SP in Oracle:

DECLARE
CUR1 SYS_REFCURSOR;
CUR2 SYS_REFCURSOR;
BEGIN
TestOracleSP (1,2,3,CUR1,CUR2) ;
END;

.: Execute in Code using FromSQL Method:

 public virtual DashBoardData GetDashBoardData(short parentSiteId, short siteId, int loggedUserId)
{

try
        {


            OracleParameter p1 = new OracleParameter("p_ParentSiteId", parentSiteId);
            OracleParameter p2 = new OracleParameter("p_SiteId", siteId);
            OracleParameter p3 = new OracleParameter("p_LoggedUserId", loggedUserId);
            OracleParameter p4 = new OracleParameter("CUR", OracleDbType.RefCursor, ParameterDirection.Output);
            OracleParameter p5 = new OracleParameter("CUR2", OracleDbType.RefCursor, ParameterDirection.Output);

            string sql = "BEGIN GET_DEPT_PROC(:p_ParentSiteId, :p_SiteId, :p_LoggedUserId, :CUR2, :CUR);END;";
            var res = context.Set<DashBoardData>().FromSql(sql, p1, p2, p3, p4, p5).ToList();

            return res[0];

        }
        catch (Exception x)
        {
            XLogger.Error("Exception : " + x);
            return null;
        }
  }
1
8/22/2019 8:53:46 PM


Related Questions





Related

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