Core Web API With Oracle Database and Dapper
Core Web API With Oracle Database and Dapper
Core Web API With Oracle Database and Dapper
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 rst, 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.
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.
Now, click OK. It will take a few minutes to con gure ASP.NET Core Web API project.
To create database and tables for this demonstration, we are using Oracle Developer Tools. It is
very lightweight and exible which helps us to work with databases smoothly.
As per Oracle
https://www.c-sharpcorner.com/article/asp-net-core-web-api-with-oracle-database-and-dapper/ 1/12
21/1/2020 ASP.NET Core Web API With Oracle Database And Dapper
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 "TEST_DB" database.
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.
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.
Now we're going to create one SP which will get the individual record for an employee based on
their employee id.
Open "Package Manager Console" from the "Nuget Package Manager" of Tools menu and type
the following command and press enter to install dapper and its dependencies if have.
After installation, you can check with references section of the project. One reference as "Dapper"
has added inside that.
We are using Asp.Net Core Web API application with Oracle and need to access Oracle database
from the Core application. To use Oracle database with .Net Core application, we have Oracle
library which will help us to manage the logic of database access. So, we have to install the
following package that is in beta version.
Now we have everything ready related to the database like the database, tables, and SPs etc. To
access the database from Web API, we have to create connection string as usual inside the
"appsettings.json" le.
01. {
02. "Logging": {
03. "IncludeScopes": false,
04. "Debug": {
05. "LogLevel": {
06. "Default": "Warning"
07. }
08. },
09. "Console": {
10. "LogLevel": {
11. "Default": "Warning"
12. }
13. }
14. },
15. "ConnectionStrings": {
16. "EmployeeConnection": "data source=mukesh:1531;password=**********;user
17. }
https://www.c-sharpcorner.com/article/asp-net-core-web-api-with-oracle-database-and-dapper/ 3/12
21/1/2020 ASP.NET Core Web API With Oracle Database And Dapper
Become a member
C# Corner Login
Create Repositories Post Ask Question
To keep the separation of concern in mind, we are using 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 implement to IEmployeeRepository.
https://www.c-sharpcorner.com/article/asp-net-core-web-api-with-oracle-database-and-dapper/ 4/12
21/1/2020 ASP.NET Core Web API With Oracle Database And Dapper
25. dyParam.Add("EMP_DETAIL_CURSOR",
Top 10 Cloud Service Providers OracleDbType.RefCursor, Par
26.
27. var conn = this.GetConnection(); Become a member
C# Corner Login
28. if (conn.State == ConnectionState.Closed)
29. { Post Ask Question
30. conn.Open();
31. }
32.
33. if (conn.State == ConnectionState.Open)
34. {
35. var query = "USP_GETEMPLOYEEDETAILS";
36.
37. result = SqlMapper.Query(conn, query, param: dyParam, co
38. }
39. }
40. catch (Exception ex)
41. {
42. throw ex;
43. }
44.
45. return result;
46. }
47.
48. public object GetEmployeeList()
49. {
50. object result = null;
51. try
52. {
53. var dyParam = new OracleDynamicParameters();
54.
55. dyParam.Add("EMPCURSOR", OracleDbType.RefCursor, ParameterDi
56.
57. var conn = this.GetConnection();
58. if(conn.State == ConnectionState.Closed)
59. {
60. conn.Open();
61. }
62.
63. if (conn.State == ConnectionState.Open)
64. {
65. var query = "USP_GETEMPLOYEES";
66.
67. result = SqlMapper.Query(conn, query, param: dyParam, co
68. }
69. }
70. catch (Exception ex)
71. {
72. throw ex;
73. }
74.
75. return result;
76. }
77.
78. public IDbConnection GetConnection()
79. {
https://www.c-sharpcorner.com/article/asp-net-core-web-api-with-oracle-database-and-dapper/ 5/12
21/1/2020 ASP.NET Core Web API With Oracle Database And Dapper
80. varCloud
Top 10 connectionString
Service Providers = configuration.GetSection("ConnectionStrin
81. var conn = new OracleConnection(connectionString);
82. return conn; Become a member
C# Corner Login
83. }
84. } Post Ask Question
85. }
86.
87. public IDbConnection GetConnection()
88. {
89. var connectionString = configuration.GetSection("ConnectionStrings").Ge
90. var conn = new OracleConnection(connectionString);
91. return conn;
92. }
To use Oracle datatypes with .Net Core, we are using OracleDyamicParameters class which will
provide the list of functions to manage Oracle parameters behaviors.
36. ((SqlMapper.IDynamicParameters)dynamicParameters).AddParameters(
Top 10 Cloud Service Providers
37.
38. Become a member
C# Corner
var oracleCommand = command as OracleCommand; Login
39.
40. if (oracleCommand != null) Post Ask Question
41. {
42. oracleCommand.Parameters.AddRange(oracleParameters.ToArray()
43. }
44. }
45. }
46. }
To access the dependencies on the controller or repository classes, we have to con gure or we
can say register our dependency classes with interfaces inside the Con gureServices method of
Startup class.
Now it's time to nally create API call in EmployeeControler. First, we have added
IEmployeeRepository inside the constructor to use dependencies. Secondly, we have to create API
call with Route attribute for both methods.
Now we have everything ready, like repository is ready, connection with Oracle database is ready
and nally, API call is also ready inside the controller. So, it's time to run the API and see the result
https://www.c-sharpcorner.com/article/asp-net-core-web-api-with-oracle-database-and-dapper/ 8/12
21/1/2020 ASP.NET Core Web API With Oracle Database And Dapper
Become a member
C#toCorner Login
To test in PostMan, rst, choose "Get" as a method and provide the URL get the list of
employee records and click to SEND button which will make a request
Post to our API
Ask and get the list
Question
of employees which we have added at the beginning while creating the database scripts.
ASP.NET Core
To get the single employee record, just pass the following URL as you can see in the image. You
can see here, we want to see the record for employee id 103. Once you send the request, you can
see the output something like as below.
ASP.NET Core
Conclusion
So, today, we have learned how to create ASP.NET Core Web API project and use Dapper with
Oracle database.
I hope this post will help you. Please put your feedback using comment which helps me to
improve myself for next post. If you have any doubts, please ask your doubts or query in
the comment section and if you like this post, please share it with your friends.
In this article, you will learn how to create an ASP.NET Core based Web API using
DapperMicro ORM with Visual Studio 2017 along with dependency injection
Mukesh Kumar
I am a software developer, Microsoft MVP, C# Corner MVP, blogger and you can also nd me
on Asp.Net Forums . I have extensive experience with designing and developing enterprise
scale applications on Microsoft technolo... Read more
http://www.mukeshkumar.net https://in.linkedin.com/in/mukeshkumartech
https://www.c-sharpcorner.com/article/asp-net-core-web-api-with-oracle-database-and-dapper/ 9/12
21/1/2020 ASP.NET Core Web API With Oracle Database And Dapper
Become a member
C# Corner Login
6 4
Post Ask Question
Type your comment here and press Enter Key (Minimum 10 characters)
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.
suresh kumar Oct 18, 2019
1816 66 0 0 0 Reply
How one use oracle change noti cation to automatically get newest records when database changes?
Mihai Dumitru Mar 02, 2019
1865 17 0 0 0 Reply
Hi, I have implemented it on vs 2017 and it works ne 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.
Omer Obaid Dec 29, 2018
1858 24 0 0 0 Reply
Good job, Mukesh! Could you please provide the source code? Thank you.
Daniel Ha Nov 12, 2018
1877 5 0 0 0 Reply
https://www.c-sharpcorner.com/article/asp-net-core-web-api-with-oracle-database-and-dapper/ 10/12
21/1/2020 ASP.NET Core Web API With Oracle Database And Dapper
Become a member
C# Corner Login
FEATURED ARTICLES
Introduction To MongoDB Atlas
Null Value And Null Reference Handling - C#6 To C# 9 New Features - Day One
Learn To Draw Simple ASP.NET Core Blazor Bar Chart Using Canvas Extensions
TRENDING UP
01 Learn Angular 8 Step By Step In 10 Days - HttpClient Or Ajax Call - Day Nine
https://www.c-sharpcorner.com/article/asp-net-core-web-api-with-oracle-database-and-dapper/ 11/12
21/1/2020 ASP.NET Core Web API With Oracle Database And Dapper
Become a member
C# Corner Login
10 Xamarin UI Test
Post View All
Ask Question
About Us Contact Us Privacy Policy Terms Media Kit Sitemap Report a Bug FAQ Partners
https://www.c-sharpcorner.com/article/asp-net-core-web-api-with-oracle-database-and-dapper/ 12/12