关键“数据源”的长度超过“128” [英] The value's length for key 'data source' exceeds it's limit of '128'

查看:594
本文介绍了关键“数据源”的长度超过“128”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一个非常类似的问题已被问到这里,但答案没有帮助我。

I know that a very similar question has been asked here, but the answer didn't help me.

我正在使用实体框架6与Oracle.ManagerDataAccess.Client 。

I am using Entity Framework 6 with the Oracle.ManagerDataAccess.Client.

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

If I define the connection string in app.config, then the connection works. If I specify the identical connection string in code, then I get the error

The value's length for key 'data source' exceeds it's limit of '128'.

这是正确的。

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

This is my connection string (with some names removed):

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

我知道有一堆空格可以删除,但我仍然不会得到该字符串低于128个字符。

I know that there are a bunch of spaces which could be removed, but I am still not going to get the string down below 128 characters.

当连接字符串在app.config中,而不是在代码中时,它的工作原理如何?

How come it works when the connection string is in app.config, but not when it is in code?

有没有可以使用的技巧,通过将一些参数卸载到另一个字符串?

Is there any trick that I can use, by offloading some of the parameters to another string?

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

I am already using a DBConfiguration object. Is there any way to set some of the parameters in that object?

如果我使用完整的oracle客户端,我想我可以引用文件tnsnames中的一个配置。 ora,但如果我们可以在没有完整客户端的情况下与oracle数据库通话,那将是一个很好的奖励。

If I use the full oracle client, I guess that I could reference a configuration in the file tnsnames.ora, but it would be a great bonus if we could talk to an oracle database without the full client.

更新

这是连接字符串在app.config中的样子

This is what the connection string looks like in 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>

在代码中,我定义了上下文类,如下所示:

In code I have defined the context class as follows:

[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")) ;
  }
}

我已经定义了一个DbConfiguration类,如下所示: p>

I have defined a DbConfiguration class as follows:

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

最后,我创建了这样的上下文

Finally, I create the context like this

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

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

The connection strings used in the two methods are identical.

推荐答案

我的同事已经找到了这个问题的答案如下:

My colleague has found an answer to this problem as follows:

将另一个构造函数添加到上下文类中以使用现有集合。

Add another constructor to the context class to use an existing collection.

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

将数据库连接创建为

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

为了完整,这是DBConfiguration类,被指定为上下文类的一个属性。

For completeness, this is the DBConfiguration class, which is specified as an attribute on the context class.

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。

This method works from a DLL without requiring any values in app.config.

这篇关于关键“数据源”的长度超过“128”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆