Primer to EntityConnectionStringBuilder

Following the spirit of Primer to ConnectionStringBuilder from ADO.NET 2.0, let’s see how the pattern has evolved in Entity Framework.

 

Building an Entity Connection String from Scratch

What’s important to keep in mind before we dive into code samples, is that an entity connection string consists of two separate connection strings – one for the EntityClient itself and one for the underlying store provider. Therefore we’ll be using two connection string builders to build a full entity connection string.

 

We need to start with the part specific to EntityClient:

            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();

            entityBuilder.Provider = Util.ReadKeyValue("Provider");

            entityBuilder.Metadata = Util.ReadKeyValue("Metadata");

 

Then we dynamically construct a DbConnectionStringBuilder for the given provider (using DbProviderFactory), and we allow the user to enter values for its keywords:

 

            DbProviderFactory dbFactory = DbProviderFactories.GetFactory(entityBuilder.Provider);

            DbConnectionStringBuilder dbBuilder = dbFactory.CreateConnectionStringBuilder();

            foreach (string key in dbBuilder.Keys)

            {

                dbBuilder.Add(key, Util.ReadKeyValue(key));

            }

 

What’s left is to set the provider-specific connection string on the EntityConnectionStringBuilder:

 

        entityBuilder.ProviderConnectionString = dbBuilder.ToString();

 

To get the connection string out of the connection string builder, you need to call the ToString() method:

 

        Console.WriteLine(entityBuilder.ToString());

 

 

The ReadKeyValue utility method that reads a line of text from the console. (Attached is the complete source code. )

 

 

Loading an Entity Connection String

EntityConnectionStringBuilder implements the concept of “named” connections. This has been a long time ask. Connection strings are built through VisualStudio wizard, or third-party tools, or by hand, and placed into the app.config/web.config file:

 

<configuration>

      <connectionStrings>

            <add name="Northwind"

                connectionString="Metadata=..\..\..\..\Northwind; Provider=System.Data.SqlClient; Provider Connection String='Data Source=.; Initial Catalog=Northwind; Integrated Security=true;'"

                providerName="System.Data.EntityClient"/>

            <add name="Adventureworks"

                connectionString="Metadata=..\..\..\..\AdventureWorks; Provider=System.Data.SqlClient; Provider Connection String='Data Source=.; Initial Catalog=AdventureWorks; Integrated Security=true;'"

                providerName="System.Data.EntityClient"/>

      </connectionStrings>

</configuration>

 

Then the application no longer needs to worry about “building” the connection string. It only needs to “load”. Like this:

 

         EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();

            entityBuilder.Name = Util.ReadKeyValue("Name");

 

 

Look up the attached source code for details.

 

Enjoy!

Program.cs