带有实体框架6的Oracle ODP.Net-TypeInitializationException-CreateCon



我创建了多个应用程序,首先使用ODP.Net,没有实体-效果很好。

        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();
    }

第二个程序是使用ODP.Net和EntityFramework,并手动创建了实体数据模型类(该类在Npgsql.EntityFramework上进行了测试,运行良好,是Oracle数据库的完美副本)。应用程序返回错误:

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()

我不知道怎么了。这是我的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>

app.config部分中存在错误:

<oracle.manageddataaccess.client>

我不得不删除:

<settings>

尽管如此,该应用程序仍然不起作用,但原因不同。

相关内容

  • 没有找到相关文章

最新更新