How to connect to an Oracle database Connection from .Net Core

David C picture David C · Jan 4, 2017 · Viewed 52.2k times · Source

Within a .netCore library I want to connect to an Oracle database. Is there any way I can do that yet?

I have tried the suggestions on another SO post, but it doesn't work, perhaps removed since? As you can see in my project.json, I'm trying to use "net461".

I'm currently trying using Oracle.ManagedDataAccess.Client via old fashioned ADO.Net. I also know that Oracle haven't bought out a .netCore connector yet. But even there I can't get it to work, it struggles to get the System.Data included, it errors whenever I try to add it.

My project.json looks like this:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Oracle.ManagedDataAccess": "12.1.24160719",
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": [
        "dnxcore50",
        "net461"
      ]
    }
  }
}

This is how I was trying to do it at the moment.

using Oracle.ManagedDataAccess.Client;

public class MyRepository
{
    public string GetServerVersion()
    {
        var _db = new OracleConnection("User Id=myUser;Password=myPassword;Data Source=MyOracleConnection");

        var serverVersion = _db.ServerVersion;
        return serverVersion;
    }
}

However the above doesn't compile as it doesn't have System.Data, which I'm struggling to import.

I'm not entrenched on any particular way of doing it, I just want the best reasonable option at this point in time.

Answer

Dubo picture Dubo · Aug 30, 2017

Beta release .Net Core Managed driver released by Oracle at the end of January 2018 http://www.oracle.com/technetwork/topics/dotnet/downloads/net-downloads-160392.html. Supported platfom mentionet in doc is now Win and Linux.

Nuget: https://www.nuget.org/packages/Oracle.ManagedDataAccess.Core

Other old alternatives with standart/instant Oracle clients :

my TestCore.csproj for last alternative :

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Mono.Data.OracleClientCore" Version="1.0.0" />
  </ItemGroup>
</Project>

My program.cs:

using System;
using System.Data.OracleClient;

namespace TestCore
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting.\r\n");                      
            using (var _db = new OracleConnection("User Id=myUser;Password=myPassword;Data Source=MyOracleConnection"))
            {
                Console.WriteLine("Open connection...");
                _db.Open();
                Console.WriteLine(  "Connected to:" +_db.ServerVersion);
                Console.WriteLine("\r\nDone. Press key for exit");
                Console.ReadKey();
            }           
        }
    }
}