5

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?

2 Answers 2

16

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

The problem is the connection strings have to be different - Entity Framework requires it in one format, while DataSets won't recognise the other.
That said, I might be able to hook the constructor to manually build up the entity connection string using the existing one...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.