The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception

158,136

Solution 1

Easiest Way!!!

  1. Right click on project and select "Manage NuGet Packages..."
  2. Search for Oracle.ManagedDataAccess. Install it.

If you are using Entity Framework and your Visual Studio Version is 2012 or higher,then

  1. Again search for Oracle.ManagedDataAccess.EntityFramework. Install it.
  2. Use below name spaces in your .cs file:
    using Oracle.ManagedDataAccess.Client;
    using Oracle.ManagedDataAccess.EntityFramework;

Its done. Now restart your visual studio and build your code.

What do these packages do?
After installing these packages no additional Oracle client software is required to be installed to connect to database.

Solution 2

Two options:

  • Install Oracle client on the PC you want to run your program on

  • Use Oracle.ManagedDataAccess.dll

You can get it on NuGet (search 'oracle managed') or download ODP.NET_Managed.zip (link is to a beta version, but points you in the right direction)

I use this so that the computers I deploy onto don't need Oracle client installed.

N.B. in my opinion this is good for console apps but annoying if you intend to install your application, so I install the client in that case.

Solution 3

Both Oracle Data Provider for .NET (from Oracle) and .NET Framework Data Provider for Oracle (from Microsoft) require Oracle Client installed on machine.

Solution 4

Ok, when you know for sure other applications that used the same process worked; on your new application make sure you have the data access reference and the three dll files...

I downloaded ODAC1120320Xcopy_32bit this from the Oracle site:

http://www.oracle.com/technetwork/database/windows/downloads/utilsoft-087491.html

Reference: Oracle.DataAccess.dll (ODAC1120320Xcopy_32bit\odp.net4\odp.net\bin\4\Oracle.DataAccess.dll)

Include these 3 files within your project:

  • oci.dll (ODAC1120320Xcopy_32bit\instantclient_11_2\oci.dll)
  • oraociei11.dll (ODAC1120320Xcopy_32bit\instantclient_11_2\oraociei11.dll)
  • OraOps11w.dll (ODAC1120320Xcopy_32bit\odp.net4\bin\OraOps11w.dll)

When I tried to create another application with the correct reference and files I would receive that error message.

The fix: Highlighted all three of the files and selected "Copy To Output" = Copy if newer. I did copy if newer since one of the dll's is above 100MB and any updates I do will not copy those files again.

I also ran into a registry error, this fixed it.

public void updateRegistryForOracleNLS()
{
    RegistryKey oracle = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\ORACLE");
    oracle.SetValue("NLS_LANG", "AMERICAN_AMERICA.WE8MSWIN1252");
}

For the Oracle nls_lang list, see this site: https://docs.oracle.com/html/B13804_02/gblsupp.htm

After that, everything worked smooth.

I hope it helps.

Solution 5

You need an Oracle Client: (before 2016) http://www.oracle.com/technology/oramag/oracle/08-nov/o68odpnet.html

(circa 2019) Renamed to "Oracle Instant Client" https://www.oracle.com/database/technologies/instant-client/downloads.html

Share:
158,136

Related videos on Youtube

Sambath Prum
Author by

Sambath Prum

A junior rails developer who keeps up learning web development.

Updated on July 09, 2022

Comments

  • Sambath Prum
    Sambath Prum almost 2 years

    I have developed an application that uses Oracle Data Provider for .NET. I copy the application file (.exe) and ODP library (Oracle.DataAccess.dll) on another computer that Oracle client and ODP.NET are NOT installed on. When I run the application, I got the error msg: The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception.

    *****************Exception Text***************
    System.TypeInitializationException: The type initlializer for 'Oracle.DataAccess.Client.OracleConnection at Oracle.DataAccess.Client.OracleConnection..cctor()
      --- End of inner exception stack trace ---
      at Oracle.DataAccess.Client.OracleConnection..ctor(String connectionString)
      ...
    

    Do I have to install ODP.NET and Oracle client on the computer that I want to run my application? If yes, is there other way that I don't have to install them but still can run my application?

    Thank you

  • Klas Mellbourn
    Klas Mellbourn about 14 years
    Note that you can also get this error on a 64 bit machine if you have only installed the 32 bit oracle client.
  • Tshilidzi Mudau
    Tshilidzi Mudau over 7 years
    Hi @tuinstoel, please note that the link you provided above is now dead.
  • tgolisch
    tgolisch almost 3 years
    Newer link added below the broken link (for reference)