6

我最近开始在我正在处理的一些项目中使用 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);
}
4

2 回答 2

4

要使用它:

var builder = new ContextBuilder<YourContext>();

using (YourContext context = builder.Create(new SqlConnection(ConfigurationManager.ConnectionStrings["yourConenctionKeyInWebConfig"].ConnectionString)))
{
     ...
}
于 2010-07-10T20:38:13.217 回答
3

据我了解,仅使用代码方法无法通过简单地将 DB 连接字符串传递给其 ctor 来实例化上下文。通常你会用它ContextBuilder来创建你的上下文。

  1. 定义ctorEntityConnection上课EntityContext

    公共实体上下文(实体连接连接):基础(连接){}

  2. 创建连接

    var factory = DbProviderFactories.GetFactory("System.Data.SqlClient"); var connection = factory.CreateConnection(); 连接.连接字符串 = 提供者连接字符串;

  3. 用于ContextBuilder创建新上下文

    var contextBuilder = new ContextBuilder(); contextBuilder.Configurations.Add(...) var context = contextBuilder.Create(connection);

于 2010-07-10T20:33:08.337 回答