首页 > 解决方案 > 在 Delphi 上使用 C# DLL 访问 Oracle DB 的问题

问题描述

我正在尝试使用 C# DLL(由我编写)通过 Delphi 访问 Oracle 数据库。我们有一个由 Delphi 编码的遗留程序(我不知道 Delphi,所以我使用 C# 编码)。我需要使用 Delphi 调用的代码 (C#) 访问 oracle 数据库。但我遇到了这个异常:'System.Data.Entity.Internal.AppConfig' 的类型初始化程序引发了异常。

我测试了在 Visual Studio 2017 中运行的代码,它运行正常,但是,当我以 DLL 格式构建时,它会抛出异常(2 个具有相同代码的不同项目:1 个库项目和 1 个控制台项目)

在调试过程中,我注意到当 DbContext 构造函数启动时代码抛出异常。

[edit1] 我在 Delphi 项目文件夹中将 app.config 重命名为 projectName.exe.config [edit1]

我的 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" />

    <!-- I tried use that, but same error -->
    <!--   <section name="oracle.manageddataaccess.client" 
             type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess"/> -->

  </configSections>
  <connectionStrings>
<!--    <add name="sqlServer" 
         connectionString="Data Source=PMUNICIPAL\SQLEXPRESS;Initial Catalog=eSocial_0.2;Integrated Security=True" 
         providerName="System.Data.SqlClient" />  -->
    <add name="oracle" 
         connectionString="DATA SOURCE=localhost:1521/xe;PASSWORD=password;USER ID=user" 
         providerName="Oracle.ManagedDataAccess.Client" />
  </connectionStrings>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="WsEnviarLoteEventos" maxReceivedMessageSize="1000000">
          <security mode="Transport">
            <transport clientCredentialType="Certificate" />
          </security>
        </binding>
        <binding name="Servicos.Empregador_ServicoConsultarLoteEventos" maxReceivedMessageSize="1000000">
          <security mode="Transport">
            <transport clientCredentialType="Certificate" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://webservices.producaorestrita.esocial.gov.br/servicos/empregador/enviarloteeventos/WsEnviarLoteEventos.svc" 
                binding="basicHttpBinding" bindingConfiguration="WsEnviarLoteEventos" 
                contract="ServicoEnviarLoteEventos.ServicoEnviarLoteEventos" name="WsEnviarLoteEventos" />
      <endpoint address="https://webservices.producaorestrita.esocial.gov.br/servicos/empregador/consultarloteeventos/WsConsultarLoteEventos.svc" 
                binding="basicHttpBinding" bindingConfiguration="Servicos.Empregador_ServicoConsultarLoteEventos" 
                contract="ServicoConsultarLoteEventos.ServicoConsultarLoteEventos" name="Servicos.Empregador_ServicoConsultarLoteEventos" />
    </client>
  </system.serviceModel>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.2.0.0" newVersion="1.2.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="Oracle.ManagedDataAccess.EntityFramework.OracleConnectionFactory, Oracle.ManagedDataAccess.EntityFramework" />
    <providers>
      <provider invariantName="Oracle.ManagedDataAccess.Client" 
                type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework" />

    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <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" />
    </DbProviderFactories>
  </system.data>
</configuration>

我的数据库上下文:

class EntityContext : DbContext
{
    public DbSet<HistoricoControle> HistoricoControles { get; set; }
    public DbSet<TabelaControle> TabelaControles { get; set; }
    public DbSet<TabelaControleStatus> TabelaControleStatuses { get; set; }

    private string dataBase;

    static string connectionString;

    //The connection string will be loaded from eSocial.config file
    //public EntityContext() : base(new OracleConnection("User Id=user;Password=password; DATA SOURCE=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVER =localhost)(SID=xe)))"), true)
    public EntityContext() : base("name=oracle")
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("user".ToUpper());
        //base.OnModelCreating(modelBuilder);
    }

}

有人知道我做错了什么吗?

标签: c#delphioracle11gentity-framework-6

解决方案


推荐阅读