Search code examples
entity-framework-6odp.net

Oracle ODP.Net With Entity Framework 6 - TypeInitializationException - CreateConnection()


I have created to apps, first with ODP.Net and without Entity - works great.

        static void Main(string[] args)
    {
        OracleConnection con = new OracleConnection();

        //using connection string attributes to connect to Oracle Database
        con.ConnectionString = "user id=****;password=****;data source=" +
            "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=server.org.net)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=ora1)))";
        con.Open();
        Console.WriteLine("Connected to Oracle" + con.ServerVersion);

        OracleCommand command = con.CreateCommand();
        command.CommandText = "SELECT ITEM FROM TEST.ORDERS";

        OracleDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine("\t{0}",
                reader[0]);
        }
        reader.Close();

        // Close and Dispose OracleConnection object
        con.Close();
        con.Dispose();
        Console.WriteLine("Disconnected");
        Console.ReadKey();
    }

Second program is using ODP.Net and EntityFramework and manaly created Entity Data Model class (which was tested on Npgsql.EntityFramework and works great, perfect copy of the database from Oracle). The application returns the error:

Exception Info: System.TypeInitializationException
Stack:
   at Oracle.ManagedDataAccess.Client.OracleClientFactory.CreateConnection()
   at System.Data.Entity.Internal.LazyInternalConnection.CreateConnectionFromProviderName(System.String)
   at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(System.Configuration.ConnectionStringSettings)
   at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(System.String, System.Data.Entity.Internal.AppConfig)
   at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
   at System.Data.Entity.Internal.LazyInternalConnection.get_ProviderName()
   at System.Data.Entity.Internal.LazyInternalContext.get_ProviderName()
   at System.Data.Entity.Internal.DefaultModelCacheKeyFactory.Create(System.Data.Entity.DbContext)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()

I don't know what is wrong. This is my app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    <section name="Oracle.ManagedDataAccess.Client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
  <system.data>
    <DbProviderFactories>
      <add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql"/>
      <remove invariant="Oracle.ManagedDataAccess.Client"/>
      <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="TestContext" connectionString="Server=localhost;Database=testDb;User Id=***;Password=***;" providerName="Npgsql"/>
    <add name="OracleDbContext" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=***;Password=***;Data Source=SampleDataSource2"/>
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb"/>
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework"/>
      <provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <publisherPolicy apply="no"/>
        <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/>
        <bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.121.2.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <oracle.manageddataaccess.client>
    <version number="*">
      <dataSources>
        <dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) "/>
        <dataSource alias="SampleDataSource2" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=server.org.net)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=ora1)))"/>
      </dataSources>
    </version>
    <settings>
      <setting name="TraceLevel" value="7" />
      <setting name="TraceFileLocation" value="C:\"/>
    </settings>
  </oracle.manageddataaccess.client>
</configuration>

Solution

  • There was a bug in app.config section:

    <oracle.manageddataaccess.client>
    

    I had to remove:

    <settings>
    

    Nevertheless, the application still does not work, but for a different reason.