我正在尝试使用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项目文件夹[edit1]中将app.config重命名为projectName.exe.config
我的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>
我的dbcontext:
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);
}
}
有人知道我在做什么错吗?