Tutorial :Can Entity Framework connection strings reuse an existing connection string?



Question:

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?


Solution: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.


Solution:2

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »