Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to Oracle database from asp.net core

First off I have read other posts about connecting to an Oracle database, unfortunately they could not help me. I'll describe what I have tried and hopefully someone can point me in the correct direction.

First off I tried to install ODTwithODAC122010 as many sources are saying,but this one gives me a few process exited with non zero exit code. It says it's successful but nothing seems to be changed.

Next to that I tried installing ODTforVS2017_122010.exe I have an updated Visual Studio professional. It says the installation is a success. This however does not seems to add any option like tutorials describe. With this I mean that there should be something visible in;

visual studio tools -> connect to database -> Oracle database, data provider menu should have something like Oracle for.. This doesn't show up unfortunately..

So I tried connecting to the database in my visual studio project selecting the .net framework data provider for oracle. Here three fields are asked: Server name username and password. I tried filling them in but it couldn't connect. For server name I used: IPADDRESS:PORT/SERVICENAME

After that I gave up hope for connecting to the database through visual studio (I can connect to the database using oracle SQL developer).

Then I installing the following nuget packages: - Oracle.ManagedDataAccess - System.Configuration.ConfigurationManager - System.Security.Permissions - System.Security.Principal

And wrote a piece of code to connect to the database like:

using(OracleConnection connect = new OracleConnection(ConnectionDetails.returnConnectionString()))
{
    connect.Open();
    Console.WriteLine("did it work?" + connect.ServerVersion);
}

Where returnConnectionString returns:

"User Id=MYID;Password=MYPASSWORD;Data Source=IPADDRESS:PORT/SERVICENAME"

Unfortunately when I try to run this I'm facing a ExecutionContext.cs not found. This is happening at connect.open();

I'm launching this code at:

    public static void Main(string[] args)
    {
        var a = new DatabaseConnector();
        a.test();
        BuildWebHost(args).Run();
    }

As I final word I want to make it clear that I'm using ASP.NET Core and that your suggestions and help will be greatly appreciated!

like image 406
S B Avatar asked Jan 28 '23 17:01

S B


2 Answers

To be precise,

  1. Right click on your project
  2. Go to Manage NuGet packages
  3. Search for Oracle.manageddataaccess.core . You will get Nuget package 2.12.0-beta2 version. Add it.
  4. In the page, import as below to access oracle classes

    using Oracle.ManagedDataAccess.Client;

This is a beta version of Oracle data access provider and PROD version is supposed to be available at the end of 3rd quarter of 2018.

like image 151
Sathishkumar Avatar answered Jan 31 '23 08:01

Sathishkumar


There's currently no ODP.NET support for .NET Core, it was suppose to be released last year, but there's been no news more than and it's coming tweet from the ODP twitter account a couple of weeks ago.

You can make the current version work by moving your project from .netcoreapp2 to a net461 or higher target framework, but it would remove the cross platform support from your project.

In order to move your project to .net461 or any other follow this steps:

  1. Edit your csproj file and replace the TargetFramework with net461
  2. If you have a web project then remove the Microsoft.AspNetCore.All package and install the Microsoft.AspNetCore (which should download the others dependencies).
like image 27
Roy Sanchez Avatar answered Jan 31 '23 08:01

Roy Sanchez