0

我知道一個非常類似的問題已被問到here,但答案並沒有幫助我。關鍵'數據源'的值長度超過了'128'的限制

我正在使用實體框架6與Oracle.ManagerDataAccess.Client。

如果我在app.config中定義連接字符串,那麼連接就可以工作。 如果我指定的代碼相同的連接字符串,然後我得到的錯誤

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

這是正確的。

這是我的連接字符串(有一些名字去掉):

"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字符。

它是如何工作的,當連接字符串在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> 

在代碼中,我定義的上下文類,如下所示:

[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類:

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

最後,創建就像在兩種方法中使用此

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

的連接串的上下文是相同的。

+0

你如何在代碼中設置連接字符串?我從來沒有見過連接字符串之前的錯誤。 – bhmahler

+0

我已添加更多信息。 –

回答

1

我的同事發現了一個回答這個問題,如下所示:

添加另一個構造上下文類使用現有的集合。

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類,它被指定爲上下文類中的一個屬性。

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中的任何值。

0

您不需要任何Oracle客戶端即可使用tnsnames.ora文件。

請參閱this answer(最後一段)文件夾ODP.NET託管驅動程序預計tnsnames.ora,分別爲。 sqlnet.ora文件。

你也可以定義別名.config文件,請參閱Configuring Oracle Data Provider for .NET

+0

我想避免使用app.config文件,因爲我想封裝類庫中的數據訪問,這將被多個應用程序使用。數據庫連接是獨立配置和存儲的。 –

+0

您不必使用app.config文件,它只是一個選項。通過給定的可能性之一指定tnsnames.ora文件的位置,然後就完成了。 –

+0

謝謝,我試了一下,失敗了。我會再試一次。 –