29

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.

Community
  • 1
  • 1
David C
  • 2,766
  • 6
  • 29
  • 44

10 Answers10

14

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();
            }           
        }
    }
}
Dubo
  • 381
  • 3
  • 7
  • I get this error when using the mono version: Unable to load DLL 'oci': The specified module could not be found. (Exception from HRESULT: 0x8007007E) – andrecarlucci Oct 03 '17 at 17:36
  • • The path to the Oracle Client must be set. • The same version of the 32bit / 64bit Oracle client as the .Net Core must be used • For Linux, nuget package Mono.Data.OracleClientCore.Linux must be used – Dubo Oct 04 '17 at 20:37
  • Oh, I thought I would be able to get rid of the Oracle Client like in the managed version. So sad. Thanks. – andrecarlucci Oct 05 '17 at 18:59
  • Just tested the beta .Net Core Managed driver (on Windows) and so far it's working pretty well, can't wait for the official release! – Rémi Gaudin Mar 01 '18 at 17:26
12

Oracle published the official Data Provider for .NET Core on nuget.

Here is a basic example to show how to use it:

using Oracle.ManagedDataAccess.Client;

public void Execute(string queryString, string connectionString)
{
    using (OracleConnection connection = new OracleConnection(connectionString))
    {
        OracleCommand command = new OracleCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}

Basically you can use it exactly like the official .NET System.Data.SqlClient (easy to find online tutorials for this) and just replace everywhere in the code SqlConnection with OracleConnection and SqlCommand with OracleCommand.

Rémi Gaudin
  • 414
  • 10
  • 14
  • anyone can share how to use this package on NET Core 3.0 MVC? – Fairuz Sulaiman Nov 26 '19 at 08:19
  • @Fairuz Sulaiman I just edited my answer to add a basic example that can help you to start. – Rémi Gaudin Nov 26 '19 at 12:20
  • thanks for your respons. Which files need to edit ? Startup.cs or Program.cs? – Fairuz Sulaiman Nov 27 '19 at 08:59
  • @Fairuz Sulaiman Actually you can use this code in any part of your ASP.NET Core application. But if you want you can add a service in Startup.cs that instantiate a class with public functions such as Execute(). So then after you can use dependency injection in any file of your application to inject your class instance that can then be used to do all your DB queries. – Rémi Gaudin Nov 27 '19 at 10:18
7

Oracle plans to certify ODP.NET, Managed Driver on Microsoft .NET Core around the end of calendar year 2017.
Oracle intends to support managed ODP.NET on .NET Core on Windows operating systems and Oracle Linux. Managed ODP.NET may support additional operating systems. Oracle will continue to evaluate support for other Linux distributions and will announce additions to the certification list at a future time. Oracle does not plan to certify on earlier versions than Microsoft .NET Core 2.0. .NET Core 2.0 contains numerous features that make managed ODP.NET certification possible on the framework

From this article :http://www.oracle.com/technetwork/topics/dotnet/tech-info/odpnet-dotnet-core-sod-3628981.pdf

Updated: Beta released ODP.NET Core

5

If you are using oracle database with .NET core then need to install few nuget packages.

  1. Microsoft.EntityFrameworkCore
  2. Oracle.EntityFrameworkCore
  3. Oracle.ManagedADataAccess.Core

After this need to write some code in the configureServices method of startup class.

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<OracleDbContext>(options=>options.UseOracle(Configuration.GetConnectionString("OracleConnection")));            
}

OracleDbContext class is inherting from the DbContext class and OracleConection connection string is set up in the appSettings.json file.

OracleDbContext.cs

public class OracleDbContext : DbContext
{
   public OracleDbContext(DbContextOptions options):base(options){}
}

appSettings.json

{
"ConnectionStrings":{
   "OracleConnection":"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS= 
 (PROTOCOL=TCP)(HOST={url of your database})(PORT={port})))(CONNECT_DATA= 
 {name of your database for ex: SID})));User Id={user id};password= 
 {password}:"
 }
}

Values in bracket {} needs to be replaced. This connection string is used when the database is on the server, not locally.

  • CONNECT_DATA need SERVICE_NAME also (CONNECT_DATA = (SERVICE_NAME = {database name} ) ) – Burk Aug 27 '21 at 14:18
3

As mentioned in other answers, Oracle has not yet released a package for their Managed Client, but is planned for later this year.

However, as of the release of .NET Standard 2.0, the System.Data.OracleClient library has been updated (available via NuGet). Obviously this is not an ideal solution since that library is obsolete, but it does give you something to work with - and you can just write a wrapper and swap it out for the official Oracle library when it is released.

Owen
  • 31
  • 2
  • 1
    I get this error using the System.Data.OracleClient: Unable to load DLL 'oci': The specified module could not be found. (Exception from HRESULT: 0x8007007E) – andrecarlucci Oct 03 '17 at 17:37
  • Added a comment, then realised I had the answer. It seems this requires an oracle client installed. Works fine on my Windows machine (where I have one) but not on linux (where I don't currently). Will try to install there and see if it then works – Adam Oct 28 '17 at 18:54
  • Well, I installed the latest Oracle instant Client on my CentOS machine and tried again - same problem. Tried copying oci.dll from my windows machine into the root folder for the API on linux (without much hope since it's a windows .dll, but you never know) but that didn't help either. Has anyone got System.Data.OracleCLient working under linux with DotNetCore 2? – Adam Oct 28 '17 at 19:32
3

Add appsettings.json to project (output dir: copy always). fill connection strings:

{
 "ConnectionStrings": {
   "connection-db": "Data Source=192.168.1.3:1521/ORACLEVM;User 
                     Id=userId;Password=123;Validate Connection=true;"
  }
}

Open Manage Nuget Packages in project, add these packages:

Microsoft.Extensions.Configuration.Json (to use configuration manager in .net core)

Oracle.ManagedDataAccess.Core (.net core version of oracle data access client)

    static void Main(string[] args)
    {
        var builder = new ConfigurationBuilder()
           .SetBasePath(Directory.GetCurrentDirectory())
           .AddJsonFile("appsettings.json");

        var configuration = builder.Build();
        var connectionString = configuration.GetConnectionString("connection-db");
        using (OracleConnection connection = new OracleConnection(connectionString))
        {
            var command = new OracleCommand("INSERT INTO ..", connection);
            connection.Open();
            command.ExecuteNonQuery();
        }
    }
ozanmut
  • 2,898
  • 26
  • 22
2

You should delete "dnxcore50" (This means your project is not a pure .net core application any more) from project.json under frameworks node and try again. As far as I know you can not connect to oracle via .net core FOR NOW, maybe checking this link can helpful

MSK
  • 1,035
  • 1
  • 10
  • 19
  • I have now deleted that, but as you suggest, it still leaves me unable to connect to Oracle. Looks like a separate API for now using old style. – David C Jan 05 '17 at 08:02
2

Based on Oracle .NET team They have released a new beta ODP.​NET Core;

You can also find it here

Mlle 116
  • 1,149
  • 4
  • 20
  • 53
1

finally devart's dotConnect for Oracle support .net core since version "9.4.280".

Ersin Tarhan
  • 321
  • 3
  • 11
0

We have done this by setting up an oracle linked server and using stored procedures that make calls to the Oracle linked server from SQL server. You might try that. You can use Openquery or Exec .. At [likedserver] to make the querys execute on the Oracle side.