C# 值';键的长度';数据源';超过它';s的限制为';128';

C# 值';键的长度';数据源';超过它';s的限制为';128';,c#,oracle,ef-code-first,entity-framework-6,connection-string,C#,Oracle,Ef Code First,Entity Framework 6,Connection String,我知道有人问了一个非常类似的问题,但答案对我没有帮助 我正在Oracle.ManagerDataAccess.Client中使用Entity Framework 6 如果我在app.config中定义了连接字符串,那么连接就工作了。 如果我在代码中指定相同的连接字符串,那么我会得到错误 The value's length for key 'data source' exceeds it's limit of '128'. 这是正确的 这是我的连接字符串(删除了一些名称): 我知道有很多空格可

我知道有人问了一个非常类似的问题,但答案对我没有帮助

我正在Oracle.ManagerDataAccess.Client中使用Entity Framework 6

如果我在app.config中定义了连接字符串,那么连接就工作了。 如果我在代码中指定相同的连接字符串,那么我会得到错误

The value's length for key 'data source' exceeds it's limit of '128'.
这是正确的

这是我的连接字符串(删除了一些名称):

我知道有很多空格可以删除,但我还是不打算把字符串降到128个字符以下

为什么当连接字符串在app.config中,而不是在代码中时,它会工作

通过将一些参数卸载到另一个字符串,我可以使用什么技巧

我已经在使用DBConfiguration对象。有没有办法设置该对象中的某些参数

如果我使用完整的oracle客户机,我想我可以引用文件tnsnames.ora中的配置,但是如果我们可以在没有完整客户机的情况下与oracle数据库通信,这将是一个巨大的好处

更新

这是app.config中连接字符串的外观

<connectionStrings>
  <add name="OracleDbContext" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=xxxxxxxxxxx;Password=xxxx;Data Source=( DESCRIPTION = ( ADDRESS_LIST = ( ADDRESS = (PROTOCOL = TCP)(HOST = VS-ORACLE.xxxxxxxx.de)(PORT = 1521) ) ) ( CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = orcl.xxxxxxxx.de) ) )" />
</connectionStrings>
最后,我创建这样的上下文

string ConnectionString = "User Id=xxxxxxxxxxx;Password=xxxx;Data Source=( DESCRIPTION = ( ADDRESS_LIST = ( ADDRESS = (PROTOCOL = TCP)(HOST = VS-ORACLE.xxxxxxxx.de)(PORT = 1521) ) ) ( CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = orcl.xxxxxxx.de) ) )" ;

using (var ctx = new GlobalAttributeContext(ConnectionString))
{
  var globalAttributes = ctx.GlobalAttributes.ToList() ;
  foreach ( GlobalAttribute ga in globalAttributes )
  {
    Console.WriteLine ( "Name: {0}, Value: {1}", ga.Attribute, ga.Value ) ;
  }
}

两种方法中使用的连接字符串是相同的。

使用tnsnames.ora文件不需要任何Oracle客户端

只需查看(最后一段)ODP.NET托管驱动程序在哪个文件夹中需要
tnsnames.ora
,resp
sqlnet.ora
文件


您也可以在
.config
文件中定义别名,请参见我的同事找到了此问题的答案,如下所示:

[DbConfigurationType(typeof(OracleDBConfiguration))]
public class GlobalAttributeContext : DbContext
{
  public DbSet<GlobalAttribute>  GlobalAttributes { get; set; }

  static GlobalAttributeContext()
  {
    Database.SetInitializer<GlobalAttributeContext>(null);
  }

  public GlobalAttributeContext(string nameOrConnectionString) : base(nameOrConnectionString)
  {
  }

  public GlobalAttributeContext() : this ( "Name=OracleDbContext" )
  {
  }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    // We have to pass the schema name into the configuration. (Is there a better way?)
    modelBuilder.Configurations.Add(new GlobalAttribute_Config_Oracle("SchemaName")) ;
  }
}
class OracleDBConfiguration : DbConfiguration
{
  public OracleDBConfiguration()
  {
    this.SetDefaultConnectionFactory ( new System.Data.Entity.Infrastructure.LocalDbConnectionFactory("v12.0") ) ;
    this.SetProviderServices ( "Oracle.ManagedDataAccess.Client", Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices.Instance ) ;
    this.SetProviderFactory  ( "Oracle.ManagedDataAccess.Client", Oracle.ManagedDataAccess.Client.OracleClientFactory.Instance ) ;
  }
}
向上下文类添加另一个构造函数以使用现有连接

public GlobalAttributeContext(DbConnection existingConnection, bool contextOwnsConnection) 
       : base(existingConnection, true)
{
}
这是完整的上下文类

namespace OracleTestExeConfigAndConnStr
{
  [DbConfigurationType(typeof(OracleDBConfiguration))]
  public class GlobalAttributeContext : DbContext
  {
    public DbSet<GlobalAttribute>  GlobalAttributes { get; set; }

    static GlobalAttributeContext()
    {
      Database.SetInitializer<GlobalAttributeContext>(null);
    }

    public GlobalAttributeContext() : base("OracleDbContext")
    {
    }

    public GlobalAttributeContext(string nameOrConnectionString)
           : base(nameOrConnectionString)
    {
    }

    public GlobalAttributeContext(DbConnection existingConnection, bool contextOwnsConnection)
           : base(existingConnection, true)
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      // We have to pass the schema name into the configuration. (Is there a better way?)
      modelBuilder.Configurations.Add(new GlobalAttribute_Config_Oracle("SchemaName")) ;
    }
  }
}
为完整起见,这是DBConfiguration类,它被指定为上下文类上的一个属性

class OracleDBConfiguration : DbConfiguration
{
  public OracleDBConfiguration()
  {
    this.SetDefaultConnectionFactory ( new System.Data.Entity.Infrastructure.LocalDbConnectionFactory("v12.0") ) ;
    this.SetProviderServices ( "Oracle.ManagedDataAccess.Client", Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices.Instance ) ;
    this.SetProviderFactory  ( "Oracle.ManagedDataAccess.Client", Oracle.ManagedDataAccess.Client.OracleClientFactory.Instance ) ;
  }
}

此方法通过DLL工作,不需要app.config中的任何值。

一旦我没有足够的声誉对任何问题或答案发表评论,我将在这里发布我的发现

就我而言,Phill Jollans的回答几乎解决了问题。以下细节必须另外完成

  • 我不需要那么多的建设者;在上下文类中,只有接收connectionstring并将其传递给基类的构造函数才足够
  • 我必须手动删除app.config文件中的SqlServer连接字符串提供程序
很可能上下文试图实例化与SqlServer而不是Oracle的连接,导致验证引发此异常。
oracle数据源密钥似乎没有这样的限制。

如何在代码中设置连接字符串?我以前从未见过连接字符串出现这种错误。我添加了更多信息。我希望避免使用app.config文件,因为我希望将数据访问封装在类库中,该类库将由多个应用程序使用。数据库连接是独立配置和存储的。您不必使用app.config文件,它只是一个选项。根据给定的可能性之一指定tnsnames.ora文件的位置,您就完成了。谢谢,我简单地尝试了一下,但失败了。我将重试。请尝试环境变量TNS_ADMIN,该值应优先于调试器输出窗口中的所有其他值。在System.Data.dll中,我收到大量异常抛出的消息:“System.Data.SqlClient.SqlException”,这使我认为它正在尝试连接到SQL server,而不是Oracle。app.config中带有连接字符串的版本确实指定了提供程序名称,当我仅在代码中使用连接字符串时,该名称将丢失。我认为它将来自DBConfiguration,我已将其指定为上下文类的一个属性。
string connStr = @"User Id=xxxxxxxxxxx;Password=xxxx;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=VS-ORACLE.xxxxxxxx.de)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl.xxxxxxxx.de)))";

using (var connection = new OracleConnection() { ConnectionString = connStr })
{
  connection.Open();
  using (var ctx = new GlobalAttributeContext(connection, true))
  {
    var globalAttributes = ctx.GlobalAttributes.ToList();
    foreach (GlobalAttribute ga in globalAttributes)
    {
      Console.WriteLine("Name: {0}, Value: {1}", ga.Attribute, ga.Value);
    }
  }
}
class OracleDBConfiguration : DbConfiguration
{
  public OracleDBConfiguration()
  {
    this.SetDefaultConnectionFactory ( new System.Data.Entity.Infrastructure.LocalDbConnectionFactory("v12.0") ) ;
    this.SetProviderServices ( "Oracle.ManagedDataAccess.Client", Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices.Instance ) ;
    this.SetProviderFactory  ( "Oracle.ManagedDataAccess.Client", Oracle.ManagedDataAccess.Client.OracleClientFactory.Instance ) ;
  }
}