我最近已開始將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);
}
你能後生成的字符串?或者執行這段代碼時它已經炸燬了嗎? – 2010-07-10 08:20:08
@亨克:添加了連接字符串。 – jrista 2010-07-10 08:26:24