English 中文(简体)
B. 实体框架诉4
原标题:Entity Framework v4 Code-Only Connection String

我最近开始使用《EF诉4法》只为我正在从事的一些项目的图书馆。 然而,我已经走进了一条na子。 我看不出连接铺设的适当格式。 我使用了以下法典来建立连接线:

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:UsersJon RistaTFSAdvanced Service ManagementTrunkMainSourceASM.DataEntityContext.cs:line 16
   at Specifications_for_EntityContext.Behaves_like_EntityContext_connected_to_a_database.TestableEntityContext..ctor(String connectionString) in C:UsersJon RistaTFSAdvanced Service ManagementTrunkMainSourceTests.ASM.DataEntityContextFacts.cs:line 165
   at Specifications_for_EntityContext.Behaves_like_EntityContext_connected_to_a_database.InitializeContext() in C:UsersJon RistaTFSAdvanced Service ManagementTrunkMainSourceTests.ASM.DataEntityContextFacts.cs`e`enter code here`nter code here`:line 160
   at ASM.Testing.xUnit.ObservationCommand.Execute(Object testClass) in C:UsersJon RistaTFSAdvanced Service ManagementTrunkMainSourceASM.Testing.xUnitObservationCommand.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"


<<>Working Example (根据已接受的答复):

有必要利用“背景”来创造任何一种使用一种只使用代码的方式的背景。 对于寻求解决同一问题的人来说,这是一个行之有效的例子:

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);
}
最佳回答

使用:

var builder = new ContextBuilder<YourContext>();

using (YourContext context = builder.Create(new SqlConnection(ConfigurationManager.ConnectionStrings["yourConenctionKeyInWebConfig"].ConnectionString)))
{
     ...
}
问题回答

就我所知,只有采用守则的方法,你才能直截了当地情况,简单地把亚洲开发银行连接到其轨道上。 通常,你将使用<代码>ContextBuilder来创造你的背景。

  1. Definectortake EntityConnection in EntityContext

    public EntityContext(EntityConnection connection): base(connection) { }

  2. 建立联系

    var factory = DbProviderFactories.GetFactory("System.Data.SqlClient"); var connection = factory.CreateConnection(); connection.ConnectionString = providerConnectionString;

  3. 使用<条码> 创造新环境

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...