views:

826

answers:

2

In my configuration files I have a connection string used by a legacy part of the app (using Datasets) and another string for Entity Framework:

<connectionStrings>
    <add name="Database" connectionString="Server=..." />
    <add name="Entities" connectionString="metadata=.....connection string='Server=..." />
</connectionStrings>

This means the server name, database name etc. are specified twice. I'd like to tell the EF connection string to reuse the first string - is this possible?

+1  A: 

I think a better approach would be to refactor the application to use just one connection string rather than trying to reference one from the other in your configuration file.

Andrew Hare
The problem is the connection strings have to be different - Entity Framework requires it in one format, while DataSets won't recognise the other.
Paul Stovell
That said, I might be able to hook the constructor to manually build up the entity connection string using the existing one...
Paul Stovell
Ah - I see what you mean.
Andrew Hare
+2  A: 

I know this post is a bit old, but I figure this will help someone out there:

You can use the EntityConnectionStringBuilder to build your EF connection from your existing connection string. This is a sample I'm using in my own code:

public static string GetEntityFrameworkConnectionString(string clientConnectionString)
    {
        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.Provider = "System.Data.SqlClient";
        entityBuilder.ProviderConnectionString = clientConnectionString;
        entityBuilder.Metadata = "res://*/Entities.UBTEntities.csdl|res://*/Entities.UBTEntities.ssdl|res://*/Entities.UBTEntities.msl";
        return entityBuilder.ToString();
    }

So when you instantiate your EF provider, just pass in the string returned from the method above into the constructor.

Hope this helps,

Sebastian G. (www.istardev.com)

Sebastian Gvirtzman