This topic is applicable to Entity Framework v1 - v6. It is not applicable to Entity Framework Core because Entity Developer generates ADO.NET code for invoking stored procedures in this case.
In this chapter we consider a database and model creation and their usage with ORM tools.
In order to complete this walkthrough, you need to create the DEPT table and a set of stored routines after this we will create the Entity Framework model using Entity Developer or Microsoft EDM Wizard with this table in the model. See the script for the table and the routines below. In this walkthrough we will use the SCOTT schema for our database objects.
CREATE TABLE DEPT ( DEPTNO NUMBER(9), DNAME VARCHAR2(20), LOC VARCHAR2(20), CONSTRAINT PK_DEPT PRIMARY KEY (DEPTNO) ); / CREATE SEQUENCE DEPT_SEQ; / CREATE TRIGGER FOR_DEPT BEFORE INSERT ON DEPT FOR EACH ROW BEGIN SELECT DEPT_SEQ.NEXTVAL INTO :NEW.DEPTNO FROM DUAL; END; / CREATE PROCEDURE GET_DEPT_PROC(curParam OUT SYS_REFCURSOR) IS BEGIN OPEN curParam FOR SELECT * FROM DEPT ORDER BY DEPTNO; END; / CREATE FUNCTION GET_DEPT_FUNC RETURN SYS_REFCURSOR AS cur SYS_REFCURSOR; BEGIN OPEN cur FOR SELECT * FROM DEPT ORDER BY DEPTNO; RETURN cur; END; / CREATE PROCEDURE DEPT_UPDATE(pDEPTNO INT, pDNAME VARCHAR, pLOC VARCHAR) IS BEGIN UPDATE DEPT SET DNAME = pDNAME, LOC = pLOC WHERE DEPTNO = pDEPTNO; END; / CREATE PROCEDURE DEPT_DELETE(pDEPTNO INT) IS BEGIN DELETE FROM DEPT WHERE DEPTNO = pDEPTNO; END; / CREATE PROCEDURE DEPT_INSERT(pDNAME VARCHAR, pLOC VARCHAR, curParam OUT SYS_REFCURSOR) IS OUT_DEPTNO NUMBER; BEGIN INSERT INTO DEPT (DNAME, LOC) VALUES(pDNAME, pLOC) RETURNING DEPTNO INTO OUT_DEPTNO; OPEN curParam FOR SELECT OUT_DEPTNO AS "OUT_DEPTNO" FROM DUAL; END dept_insert; / INSERT INTO DEPT (DNAME, LOC) VALUES ('ACCOUNTING', 'NEW YORK'); INSERT INTO DEPT (DNAME, LOC) VALUES ('RESEARCH', 'DALLAS'); INSERT INTO DEPT (DNAME, LOC) VALUES ('SALES', 'CHICAGO'); INSERT INTO DEPT (DNAME, LOC) VALUES ('OPERATIONS', 'BOSTON');
Note: |
---|
For Entity Developer we did not select the stored routines because in Entity Developer it is very easy to add them later. In Microsoft EDM Designer you may use the Update Wizard to add a stored procedure into an existing model, but remember that Update Wizard overwrites the storage model and may discard your manual changes. So, it is better to add everything we need when creating model. |
Entity Framework Tutorial | Using Entity Data Model Wizard | Stored Routines in Entity Framework Model | Stored Procedure Returning Result Set using REF CURSOR Out Parameter | Stored Function Returning REF CURSOR | Mapping CUD Operations to Stored Routines