Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provider connection string from Entity Framework

Tags:

If you are using object contex data model (with EDMX file), during its creation you might want to specify the connection string inside your config file.

The connection string is unfortunately not the common connection string as it contains some ...things needed for the entity connections. Example with MySql connection:

<add name="MyDbEntities" connectionString="metadata=res://*/Namespace.MyDb.csdl|res://*/Namespace.MyDb.ssdl|res://*/Namespace.MyDb.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=172.17.17.154;User Id=user;password=password;Persist Security Info=True;database=MyDatabase;Convert Zero Datetime=true&quot;" providerName="System.Data.EntityClient" />

The problem I have is that this connection string contains the connection string of the provider in the parameter "provider connection string".

For a specific reason, I need to create a new MySqlConnection, unrelated to the entity model. For creating the MySqlConnection, I need to provide it the mysql connection string - which is the provider connection string for the entity model and I know the connection string I need is always the same connection string for the entity model.

But how do I get the provider connection string programmaticaly? I was stuck with browsing the model instance with no success...

The following:

ModelInstance.Connection.ConnectionString

contains something like "name=TestBotEntities", not even the whole connection string. So I tried:

ConfigurationManager.ConnectionStrings["MyDbEntities"].ConnectionString

but that one contains the whole entity connection string and I just don't know how to parse it, how to get only the provider connection string from it.

like image 818
Mirek Avatar asked Aug 07 '12 07:08

Mirek


People also ask

How do I change the connection string in Entity Framework?

If you want to change the connection string go to the app. config and remove all the connection strings. Now go to the edmx, right click on the designer surface, select Update model from database, choose the connection string from the dropdown, Click next, Add or Refresh (select what you want) and finish.

What is provider name in connection string?

The providerName attribute is used to set the name of the . NET Framework data provider that the DataSource control uses to connect to an underlying data source. If no provider is set, the default is the ADO.NET provider for Microsoft SQL Server.

How does Entity Framework connect to database?

Database First Approach creates the Entity Framework from an existing database. It creates model codes from the database. The database in the project and those classes become the link between the database and controller.


1 Answers

Turns out there are two ways.

I could parse the entity connection string via the EntityConnectionStringBuilder:

string entityConnectionString = ConfigurationManager.ConnectionStrings["MyDbEntities"].ConnectionString;
string providerConnectionString = new EntityConnectionStringBuilder(entityConnectionString).ProviderConnectionString;

...or if I have the specific model instance available, I can get it from here.

((System.Data.EntityClient.EntityConnection)ModelInstance.Connection).StoreConnection.ConnectionString;
like image 165
Mirek Avatar answered Sep 21 '22 15:09

Mirek