我最近开始在我正在处理的一些项目中使用 EF v4 Code Only 库。但是,我遇到了一些障碍。我似乎无法找出连接字符串的正确格式。我使用以下代码来构建连接字符串:
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)
看到我没有任何元数据......因为我只使用代码,所以我有点束缚。非常感谢任何见解。
构建器类生成的连接字符串如下:
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);
}