Compartir a través de


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

Comments

  • Anonymous
    March 20, 2008
    I'm having a problem with how connection strings seem to work in practice. We have a solution containing a library project with the business logic and the entity services/model.  The interfaces to it will be exposed through a separate web services project (WCL).  To test the business layer/data layer library I have a test project (standard VS2008 test project). I can't get any tests to run because: "The specified named connection is either not found in the configuration, not intended to be used with the EntityClient Provider, not valid.."  The DB connection is correct. Entity  paths point to the ssdl, msl and csdl files correctly from the dll build directory. Haven't tried testing from the WCF layer. What doesn't seem to make a difference is:
  • setting the output directory for both the tests and the library to the same directory.
  • specifying full paths for the metadata
  • copying the app.config to the test project I'd be happy to try your code above, but I'm not sure how to make it load in a library situation. Any thoughts on how we could get entity framework to work in what I think should be a fairly common architecture?