I have spent more than 2 days now and I have not been able to find something that clearly explains how to create an API in .NET Core using an oracle database as I found very little information about this topic.
I ask for some patience, I'm a noob at .net core and oracle makes it harder for me to have to use Oracle Database.
I post this with hope that somebody will be able to guide me through this process.
I followed the next tutorial:
I could successfully create the API, the problem is that this API does not work with oracle database which is a requirement I cannot change.
So far what I have done is:
- I created a .NET Core project and selected API
- I created a Models Folder and created Employee class
- Added OracleManagedDataAccessCore Nuget package
- I created a Scaffold API controller (Empty)
- I connected my api to oracle
My Employee Model code is the following:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace OracleAPI.Models
{
public class Employee
{
[Key]
public int EMPLOYEE_ID { get; set; }
[Required]
public string FIRST_NAME { get; set; }
[Required]
public string LAST_NAME { get; set; }
[Required]
public string EMAIL { get; set; }
[Required]
public string PHONE_NUMBER { get; set; }
[Required]
public string HIRE_DATE { get; set; }
public int JOB_ID{ get; set; }
public double SALARY { get; set; }
public double COMMISSION_PCT { get; set; }
public int MANAGER_ID { get; set; }
public int DEPARTMENT_ID { get; set; }
}
}
My API Controller looks something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OracleAPI.Models;
using Oracle.ManagedDataAccess.Client;
namespace OracleAPI
{
[Route("api/[controller]")]
[ApiController]
public class APIController : ControllerBase
{
public string conString = "User Id=HR;Password=HR;Data Source=localhost:1521/xe";
public static Oracle.ManagedDataAccess.Client.OracleConnection con;
public APIController()
{
con = new OracleConnection(conString);
}
//This is the part where Im stuck
[HttpGet]
public async Task<ActionResult<Employee>> GetEmployees()
{
using (con)
{
try
{
using (OracleCommand cmd = new OracleCommand())
{
con.Open();
cmd.BindByName = true;
cmd.CommandText = "SELECT * FROM employees";
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//await context.Response.WriteAsync("This part is obviously going to be removed but it's commented here to show why I chose this method to by an async task and this was in the example of the connection to the oracle database");
}
}
}
catch (Exception ex)
{
return null;
}
}
}
}
}
The problem basically comes when I have to choose a return type, I have tried using JSONResult but I have not managed to convert my reader result into Json, nor I have been able to use the same return types that are used in the tutorial when EntityFrameworkCore is used.
I hope you can guide me through the right path for understanding not only how to choose the right return type depending on what operation I'm performing (Create, Read, Update, Delete) (Post, Get, Put, Delete) and also how to be able to convert OracleCommand results into that return type.
Note: I use Oracle database 18c Express Edition and the purpose of creating the API is because I need to consume the API from a Xamarin project and since the oracle nuget is not available for iOS, the best bet for me is to create the API.
Thanks a lot in advanced. I will appreciate any help anyone can provide.