键“数据源"的值长度超过了“128"的限制 [英] The value's length for key 'data source' exceeds it's limit of '128'

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

问题描述

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

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

我正在使用带有 Oracle.ManagerDataAccess.Client 的 Entity Framework 6.

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 类如下:

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 connection.

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

作为一个单独的步骤创建数据库连接并将连接传递给上下文对象.

Create the database connection as a separate step and pass the connection into the context object.

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天全站免登陆