2010-07-10 57 views
6

我最近已開始將EF v4代碼庫用於我正在處理的一些項目。但是,我遇到了一些障礙。我似乎無法弄清楚連接字符串的正確格式。我用下面的代碼來構建連接字符串:實體框架v4僅限代碼連接字符串

string connectionString = new EntityConnectionStringBuilder 
{ 
    Provider = "System.Data.SqlClient", 
    ProviderConnectionString = new SqlConnectionStringBuilder 
    { 
     DataSource = "localhost", 
     InitialCatalog = "ASM_Testing", 
     IntegratedSecurity = true, 
     Pooling = false 
    }.ConnectionString 
}.ConnectionString; 

但是,使用它會導致以下錯誤:

Specifications_for_EntityContext.When_logging_in_application_with_valid_app_role_and_password.Login_should_be_successful : System.ArgumentException : Some required information is missing from the connection string. The 'metadata' keyword is always required. 
Stack Trace: 
    at System.Data.EntityClient.EntityConnection.ValidateValueForTheKeyword(DbConnectionOptions effectiveConnectionOptions, String keywordName) 
    at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) 
    at System.Data.Objects.ObjectContext..ctor(String connectionString) 
    at ASM.Data.EntityContext..ctor(String connectionString) in C:\Users\Jon Rista\TFS\Advanced Service Management\Trunk\Main\Source\ASM.Data\EntityContext.cs:line 16 
    at Specifications_for_EntityContext.Behaves_like_EntityContext_connected_to_a_database.TestableEntityContext..ctor(String connectionString) in C:\Users\Jon Rista\TFS\Advanced Service Management\Trunk\Main\Source\Tests.ASM.Data\EntityContextFacts.cs:line 165 
    at Specifications_for_EntityContext.Behaves_like_EntityContext_connected_to_a_database.InitializeContext() in C:\Users\Jon Rista\TFS\Advanced Service Management\Trunk\Main\Source\Tests.ASM.Data\EntityContextFacts.cs`e`enter code here`nter code here`:line 160 
    at ASM.Testing.xUnit.ObservationCommand.Execute(Object testClass) in C:\Users\Jon Rista\TFS\Advanced Service Management\Trunk\Main\Source\ASM.Testing.xUnit\ObservationCommand.cs:line 24 
    at Xunit.Sdk.FixtureCommand.Execute(Object testClass) 
    at Xunit.Sdk.BeforeAfterCommand.Execute(Object testClass) 
    at Xunit.Sdk.LifetimeCommand.Execute(Object testClass) 
    at Xunit.Sdk.TimedCommand.Execute(Object testClass) 
    at Xunit.Sdk.ExceptionAndOutputCaptureCommand.Execute(Object testClass) 

看到,因爲我沒有任何元數據...因爲我我正在使用Code Only,我有點困惑。任何見解都非常感謝。

由助洗劑類生成的連接字符串如下:

provider=System.Data.SqlClient;provider connection string="Data Source=localhost;Initial Catalog=ASM_Testing;Integrated Security=True;Pooling=False"


工作實施例(基於接受的答案)

有必要使用中,ContextBuilder創建任何使用純代碼模式的上下文實例。以下是這對於那些誰正在尋找的答案,同樣的問題的工作示例:

protected override void InitializeContext() 
{ 
    string connectionString = new SqlConnectionStringBuilder 
    { 
     DataSource = "localhost", 
     InitialCatalog = "Testing", 
     IntegratedSecurity = true, 
     Pooling = false 
    }.ConnectionString; 

    var connection = new SqlConnection(connectionString); 
    var builder = new ContextBuilder<TestableEntityContext>(); 
    m_context = builder.Create(connection); 
} 
+0

你能後生成的字符串?或者執行這段代碼時它已經炸燬了嗎? – 2010-07-10 08:20:08

+0

@亨克:添加了連接字符串。 – jrista 2010-07-10 08:26:24

回答

4

要使用它:

var builder = new ContextBuilder<YourContext>(); 

using (YourContext context = builder.Create(new SqlConnection(ConfigurationManager.ConnectionStrings["yourConenctionKeyInWebConfig"].ConnectionString))) 
{ 
    ... 
} 
+0

謝謝。我知道ContextBuilder,但它並沒有跨越我的想法,在我的EntityContext類的單元測試中使用它。我認爲這將解決問題。謝謝! – jrista 2010-07-11 00:51:50

3

據我瞭解使用代碼唯一的方法,你不能簡單地通過DB連接字符串來實例化上下文其構造函數。通常情況下,您會使用ContextBuilder來創建您的上下文。

  1. 定義構造函數在EntityContext類服用EntityConnection

    公共的EntityContext(EntityConnection連接):基座(連接) { }

  2. 創建連接

    變種工廠= DbProviderFactories.GetFactory ( 「System.Data.SqlClient的」); var connection = factory.CreateConnection(); connection.ConnectionString = providerConnectionString;

  3. 使用ContextBuilder來創建新的上下文

    變種中,ContextBuilder =新中,ContextBuilder(); (...) var context = contextBuilder.Create(connection);

+0

+1很好的答案。我讀了Gregoire的第一個,並將其標記爲已接受。不過,他們都回答了我的問題。謝謝! – jrista 2010-07-11 00:53:16

相關問題