gpt4 book ai didi

c# - Entity Framework v4 纯代码连接字符串

转载 作者:太空狗 更新时间:2023-10-29 21:30:55 28 4
gpt4 key购买 nike

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

最佳答案

使用方法:

var builder = new ContextBuilder<YourContext>();

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

关于c# - Entity Framework v4 纯代码连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3218208/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com