This article will focus on how to create ASP.NET Core Web API to get the data from an Oracle database using Dapper ORM. First things first, here, we are not using SQL because there already are so many articles available on the internet that mostly use SQL Server for demonstration. So, I thought of writing an article where we will use Oracle as a database. To reduce the complexity of the database access logic, we are using Dapper ORM. So, let's move to the practical demonstration.
Create ASP.NET Core Web API Project
To create a new project in ASP.NET Core Web API, just open Visual Studio 2017 version 15.3 and we have to follow the below steps.
- Go to the File menu and click New >> Project.
- From the New Project window, first, you have to choose .NET Framework 4.6 or above versions and then, from the left panel, choose Visual C# and then .NET Core.
- From the right panel, choose “.NET Core Web Application” provide the Save location where you want to save the project, and click OK.
- From the next window which will provide you with different kinds of templates, you have to choose Web API.
Now, click OK. It will take a few minutes to configure the ASP.NET Core Web API project.
Setup Oracle Table and Stored Procedures
To create a database and tables for this demonstration, we are using Oracle Developer Tools. It is very lightweight and flexible which helps us to work with databases smoothly.
As per Oracle
Oracle SQL Developer is a free, integrated development environment that simplifies the development and management of Oracle Database in both traditional and Cloud deployments. SQL Developer offers complete end-to-end development of your PL/SQL applications, a worksheet for running queries and scripts, a DBA console for managing the database, a reports interface, a complete data modeling solution, and a migration platform for moving your 3rd party databases to Oracle.
Create a database name call it "TEST_DB" and inside that create a table name as "EMPLOYEE". You can use the following syntax to create the table inside the "TEST_DB" database.
CREATE TABLE "TEST_DB"."EMPLOYEE"
(
"ID" NUMBER(10,0) GENERATED BY DEFAULT ON NULL AS IDENTITY
MINVALUE 1
MAXVALUE 9999999999999999999999999999
INCREMENT BY 1
START WITH 100
CACHE 20
NOORDER
NOCYCLE,
"NAME" VARCHAR2(255 BYTE),
"SALARY" NUMBER(10,0),
"ADDRESS" VARCHAR2(500 BYTE)
)
SEGMENT CREATION IMMEDIATE
PCTFREE 10
PCTUSED 40
INITRANS 1
MAXTRANS 255
NOCOMPRESS
LOGGING
STORAGE(
INITIAL 65536
NEXT 1048576
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
FREELISTS 1
FREELIST GROUPS 1
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
TABLESPACE "TEST_DATA";
We need to add some dummy records inside the tables so that we can directly get the data from PostMan. So, we are adding four records here as follows.
INSERT INTO TEST_DB.EMPLOYEE (ID, NAME, SALARY, ADDRESS) VALUES (100, 'Mukesh', 20000, 'India');
INSERT INTO TEST_DB.EMPLOYEE (ID, NAME, SALARY, ADDRESS) VALUES (101, 'Rion', 28000, 'US');
INSERT INTO TEST_DB.EMPLOYEE (ID, NAME, SALARY, ADDRESS) VALUES (102, 'Mahesh', 10000, 'India');
INSERT INTO TEST_DB.EMPLOYEE (ID, NAME, SALARY, ADDRESS) VALUES (103, 'Banky', 20000, 'India');
Now it's time to create one SP which will bring the list of employees' records. Here we are using Cursor for returning list of data as an output parameter.
CREATE OR REPLACE PROCEDURE "TEST_DB"."USP_GETEMPLOYEES"
(
EMPCURSOR OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN EMPCURSOR FOR
SELECT ID, NAME, SALARY, ADDRESS
FROM Employee;
END;
Now we're going to create one SP which will get the individual record for an employee based on their employee ID.
CREATE OR REPLACE PROCEDURE "TEST_DB"."USP_GETEMPLOYEEDETAILS"
(
EMP_ID IN INT,
EMP_DETAIL_CURSOR OUT SYS_REFCURSOR
) AS
BEGIN
OPEN EMP_DETAIL_CURSOR FOR
SELECT ID, NAME, SALARY, ADDRESS
FROM Employee
WHERE ID = EMP_ID;
END;
Install Dapper ORM
Open "Package Manager Console" from the "NuGet Package Manager" of the Tools menu type the following command and press enter to install Dapper and its dependencies if have one.
Install-Package Dapper -Version 1.50.5
After installation, you can check with references section of the project. One reference as "Dapper" has been added inside that.
Install Oracle Manage Data Access for Core
We are using the Asp.Net Core Web API application with Oracle and need to access the Oracle database from the Core application. To use the Oracle database with the .Net Core application, we have the Oracle library which will help us to manage the logic of database access. So, we have to install the following package that is in the beta version.
Install-Package Oracle.ManagedDataAccess.Core -Version 2.12.0-beta2
Add Oracle Connection
Now we have everything related to the database like the database, tables, SPs, etc. To access the database from Web API, we have to create a connection string as usual inside the "appsettings.json" file.
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"ConnectionStrings": {
"EmployeeConnection": "data source=mukesh:1531;password=**********;user id=mukesh;Incr Pool Size=5;Decr Pool Size=2;"
}
}
Create Repositories
To keep the separation of concern in mind, we are using a Repository here. Create a new folder as "Repositories" inside the Web API project and create an interface as "IEmployeeRepository" and a class as "EmployeeRepository" which will be implemented to IEmployeeRepository.
namespace Core2API.Repositories
{
public interface IEmployeeRepository
{
object GetEmployeeList();
object GetEmployeeDetails(int empId);
}
}
Following is the EmployeeRepository class which is implementing IEmployeeRepository. To access configuration, we are injecting IConfiguration in the constructor. So, we have a configuration object that is ready to use. Apart from that, we have the GetConnection() method which will get the connection string from the appsettings.json and provide it to OracleConnection to create a connection and finally return the connection. We have implemented "IEmployeeRepository" which has two methods GetEmployeeDetails and GetEmployeeList.



Özay BakırhanPosted Dec 3, 2021, 6:30 AM
Hi Kumar? Based on your code set up to execute a store procedure, how do i return an output parameter which added earlier with dyParam? for example; var dyParam = new OracleDynamicParameters(); dyParam .Add("FIRSTNAME", OracleDbType.Varchar2, ParameterDirection.Input, "MUKEHS"); dyParam .Add("LASTNAME", OracleDbType.Varchar2, ParameterDirection.Input, "KUMAR"); dyParam .Add("ID", OracleDbType.Int32, ParameterDirection.Output, null); . . var query = "INSERT_GETEMPLOYEES"; result = SqlMapper.Execute(conn, query, param: dyParam, commandType: CommandType.StoredProcedure); int id = ??????
Akash AgrawalPosted Feb 16, 2021, 12:19 PM
Want to perform the same logic but in Visual Studio 2019 ........
Marwen GhodbenPosted Apr 18, 2020, 5:46 PM
Thanks for this excellent article sir How to implement adding and modify employee logic
Jean LimaPosted Mar 15, 2020, 6:13 PM
I'm having the problem below when using this dapper calling tutorial. Could you help meError: ERROR on line 1: ORA-06550: line 1, column 7: PLS-00306: incorrect number of argument types in the call to
suresh kumarPosted Oct 18, 2019, 5:38 AM
Hi Mukesh, Thanks for giving nice article, I works good with out cursor but when I try to return Varchar2 Employee Name alone it gives 0 "Zero" output.
Mihai DumitruPosted Mar 2, 2019, 5:18 AM
How one use oracle change notification to automatically get newest records when database changes?
Omer ObaidPosted Dec 29, 2018, 5:04 AM
Hi, I have implemented it on vs 2017 and it works fine Now I want t implement it on vs 2015, I have done everything but these two functions are not available in vs 2015 Do you know any alternative of these two: UseHsts() & UseHttpsRedirection As they are not available in core 1.0 Thanks.
Daniel HaPosted Nov 12, 2018, 6:54 AM
Good job, Mukesh! Could you please provide the source code? Thank you.