Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework v4 Code-Only Connection String

I have recently begun to use the EF v4 Code Only library for some projects I am working on. However, I have run into a bit of a snag. I can't seem to figure out the proper format for the connection string. I have used the following code to build the connection string:

string connectionString = new EntityConnectionStringBuilder
{
    Provider = "System.Data.SqlClient",
    ProviderConnectionString = new SqlConnectionStringBuilder
    {
        DataSource = "localhost",
        InitialCatalog = "ASM_Testing",
        IntegratedSecurity = true,
        Pooling = false
    }.ConnectionString
}.ConnectionString;

However, using it results in the following error:

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)

Seeing as I don't have any metadata...since I am using Code Only, I'm in a bit of a bind. Any insight is greatly appreciated.

The connection string generated by the builder classes is as follows:

provider=System.Data.SqlClient;provider connection string="Data Source=localhost;Initial Catalog=ASM_Testing;Integrated Security=True;Pooling=False"


Working Example (based on accepted answers)

It is necessary to use the ContextBuilder to create any instance of a context that uses a code-only mode. Here is a working example of this for those who are searching for an answer to the same problem:

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);
}
like image 770
jrista Avatar asked Jul 10 '10 07:07

jrista


People also ask

Where is Entity Framework connection string?

The Entity Data Model tools generate a connection string that is stored in the application's configuration file.


2 Answers

To use it:

var builder = new ContextBuilder<YourContext>();

using (YourContext context = builder.Create(new SqlConnection(ConfigurationManager.ConnectionStrings["yourConenctionKeyInWebConfig"].ConnectionString)))
{
     ...
}
like image 142
Gregoire Avatar answered Sep 19 '22 12:09

Gregoire


As far as I understand using code only approach you can't instantiate context by simply passing DB connection string to its ctor. Normally you would use ContextBuilder to create your context.

  1. Define ctor taking EntityConnection in EntityContext class

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

  2. Create connection

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

  3. Use ContextBuilder to create new context

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

like image 43
Yury Tarabanko Avatar answered Sep 21 '22 12:09

Yury Tarabanko