The value#39;s length for key #39;data source#39; exceeds it#39;s limit of #39;128#39;(键“数据源的值长度超过了“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"的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:键“数据源"的值长度超过了“128"的限制
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01