Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to Oracle database in dotnet mvc application

Oracle released a beta version driver ODP for dotnet core 2(Finally!). But I can't make it work. Does anybody did it? If yes, please send me the code or help me fixing this one \o

Useful: I am using Visual Code, project "dotnet new mvc" (.net 2) and I installed Oracle.ManagedDataAccess.Client via Nuget Add Package facility (CTRL + P, ...) Here my code:

public static OracleConnection AbrirSigmaUser(AutenticacaoModel autenticacao)
        {
            try
            {

                string _connectionString;

                _connectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.15)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=databaseName)));User Id=UserName;Password=***;";


                OracleConnection conexao = new OracleConnection();
                conexao.ConnectionString = _connectionString;

                //right here the program exit
                conexao.Open();             




                return conexao;
            }
            catch (System.Exception ex)
            {               

                throw new Exception("Não foi possível conectar ao banco de dados!" + "\nErro: " + ex.Message);
            }
        }

The compiler was throwing a lot of exceptions about missing dlls, so I installed them via Nuget Add Package facility:

//required for connection
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
using System.Security.Permissions;
using System.Security.Principal;

After adding all dlls asked, the program goes to conexao.Open() and never comes back, throwing an unhandled exception:

Unhandled Exception: System.TypeLoadException: Could not load type 'System.Security.Principal.WindowsImpersonationContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
   at OracleInternal.ConnectionPool.PoolManager`3.CreateNewPRThreadFunc(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
The program '[8860] Sensatta-Analytcs.dll' has exited with code 0 (0x0).

Is all of it really needed to make a simple connection?

If there's something missing(e.g code samples) in order to figure out my issue, just tell me!

like image 560
lat94 Avatar asked Mar 07 '18 18:03

lat94


3 Answers

For me, the solution was to install the Oracle.ManagedDataAccess.Core NuGet package only. I had Oracle.ManagedDataAccess installed as well and I needed to uninstall them to fix the error.

like image 72
Nathan Raine Avatar answered Nov 02 '22 16:11

Nathan Raine


As @Lesiak stated, I used the wrong package (the right one is Oracle.ManagedDataAccess.Core.dll). But that is an error someone else might fall in too. Because when you download the zip file from Oracle Link the dll name is Oracle.ManagedDataAccess.dll instead of Oracle.ManagedDataAccess.Core.dll that's error prone.

like image 21
lat94 Avatar answered Nov 02 '22 16:11

lat94


Install Oracle.ManagedDataAccess.Core nuget package to your project.
Make sure code below is included in your .csproj file:

<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="3.21.1" />

Also uninstall "Oracle.ManagedDataAccess" from your project.
Make sure code below is removed from your .csproj file:

<PackageReference Include="Oracle.ManagedDataAccess" Version="19.10.1" />
like image 36
Mimina Avatar answered Nov 02 '22 16:11

Mimina