4

We have a need to use an old fashioned ADO database connection in one tiny part of the main entity framework application.

We can manually specify the connection string in this part of code, but given that the connection string is already present in the App.Config this seems redundant.

However when we use the configuration manager to retrieve the connection string, it brings with it all of the metadata stuff that entity framework uses.

This causes an error as ADO doesnt recognise the metadata keyword.

How can I parse this connection string to remove the metadata and just get the plain ADO connection string?

3 Answers 3

7

You can get DbConnection instance from DbContext:

var context = new YourDbContext();
var connection = context.Database.Connection;

Of course, you can get connection string from connection, but you don't need thus you can use already existing connection object.


Here is Quick Watch of connection object - as you can see it's simple ADO.NET SqlConnection object with ordinal connection string.

enter image description here

In config file I have Entity Framework connection string with metadata:

  <connectionStrings>
    <add name="NorthwindEntities"
         connectionString="metadata=res://*/Northwind.csdl|res://*/Northwind.ssdl|res://*/Northwind.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=Northwind;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
  </connectionStrings>
Sign up to request clarification or add additional context in comments.

3 Comments

This just gives me the string "name=[NameOfConnectionString]" and not the actual string
@StevenWood nope, it can't. connection is an instance of DbConnection class (with SQL server it will be SqlConnection). This is an ordinal ADO.NET connection object. And its connection string does not have any specific EF metadata or connection string id from configuration file
As I am just trying to the same as @StevenWood I stumbled over your post. And I, too, still have the EF metadadata in the ConnectionString property. Please be aware that according to MSDN the actual content is depending on the specific data source for this connection. In my case this is an old EF4 ObjectContext. You stated that you are using a DbContext, which might make a difference.
5

Below should work :

var efConn = new System.Data.EntityClient.EntityConnectionStringBuilder(efConnection);                     
string adoConn =  efConn.ProviderConnectionString;

1 Comment

this is a better approach since it does not handle all the work needed to instance dbContext, instead, internally just parses the ConnectionString. It worked like a charm with EF 6.1.2
1

I was trying to the same and ended up using this approach:

private static string RemoveEntityFrameworkMetadata(string efConnection)
{
  int start = efConnection.IndexOf("\"", StringComparison.OrdinalIgnoreCase);
  int end   = efConnection.LastIndexOf("\"", StringComparison.OrdinalIgnoreCase);

  // We do not want to include the quotation marks
  start++;
  int length = end - start;

  string pureSqlConnection = entityFrameworkConnection.Substring(start, length);
  return pureSqlConnection;
}

This may not be the most elegant solution, but it works.

(I also tried Regex but can't get my head around it.)

Comments

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.