我最近开始为我正在处理的一些项目使用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)
因为我没有任何元数据...因为我使用的是Code Only,所以我有点绑定。非常感谢任何见解。
构建器类生成的连接字符串如下:
提供商= System.Data.SqlClient的;提供者 connection string =“数据 源=本地主机;初始 目录= ASM_Testing;集成 安全= TRUE;池=假“
工作示例(基于已接受的答案)
有必要使用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 :(得分:4)
使用它:
var builder = new ContextBuilder<YourContext>();
using (YourContext context = builder.Create(new SqlConnection(ConfigurationManager.ConnectionStrings["yourConenctionKeyInWebConfig"].ConnectionString)))
{
...
}
答案 1 :(得分:3)
据我所知,只使用代码方法,您无法通过简单地将DB连接字符串传递给它的ctor来实例化上下文。通常,您可以使用ContextBuilder
来创建上下文。
在EntityConnection
班级
EntityContext
public EntityContext(EntityConnection连接):base(连接) { }
创建连接
var factory = DbProviderFactories.GetFactory(“System.Data.SqlClient”); var connection = factory.CreateConnection(); connection.ConnectionString = providerConnectionString;
使用ContextBuilder
创建新的上下文
var contextBuilder = new ContextBuilder(); contextBuilder.Configurations.Add(...) var context = contextBuilder.Create(connection);