Noich
Noich

Reputation: 15461

Entity Framework - supply a connection string to constructor

I'm using EF in my C# project. We're about to add clients which will have other databases with other connection strings.
There's no need to sync between DBs.
I tried using the following c'tor (the 2nd out of 3 supplied):

 public AppEntities(string connectionString)

and I get the following error: 'Keyword not supported: 'data source'.' The connection string is as follows:

metadata=res://EntityFramework/App.csdl|res://EntityFramework/App.ssdl|res://EntityFramework/App.msl;provider=System.Data.SqlClient;provider connection string="Data Source=dbname\dbname;Initial Catalog=App;Persist Security Info=True;User ID=User;Password=password;MultipleActiveResultSets=True"

This is the same connection string that is used when taken from App.Config. I don't want to take it from there but rather build it myself (thus leaving the username & password hard-coded in my app's code and not in free text.
Any idea?

Thanks.

Upvotes: 1

Views: 2474

Answers (1)

Bernhard Kircher
Bernhard Kircher

Reputation: 4172

The connectionstring parameter is not an Entity Framework Connectionstring (as in the .config file) but it has to be a "normal" .net connectionstring.

The link's example shows how to "convert" the ef connectionstring to a normal connectionstring (using the EntityConnectionStringBuilder.ToString() method). If you pass the app.config's unprepared string you will get the mentioned error.

You can use the EntityConnectionStringBuilder to parse the app.config value and convert it.

Hope that helps.

Upvotes: 3

Related Questions