Connection Strings - MVC Entity Framework - Database First - entity-framework

Can anyone suggest how I could programatically switch debug and live connection strings?
I've seen other people have passed an EntityConnection to the constructor from the controller like this :
private XYZDatabase db = new XYZDatabase(ConfigurationManager.
ConnectionStrings["XYZDatabase-TEST"].ConnectionString);
but it still requires manually changing it? is there a way to use
System.Net.Dns.GetHostName() or similar
to switch it automatically?
Thanks

Ayende has a post that addresses this issue.
http://ayende.com/blog/135169/frictionless-development-web-config-and-connection-strings

In my software I just have 2 connection strings of the same name in my app.config and comment/uncomment to switch between configurations.
Another method is that you could create a static property in your solution (.asax file?) and just use that to swap between XYZDatabase-TEST and XYZDatabase when you are fetching ConnectionStrings.

Add connection string to your Web.config like:
<connectionStrings>
<add name="XYZDatabase-TEST" connectionString="Server=.\SQLEXPRESS;Database=XYZDatabase-TEST";integrated security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
Then open you Web.Release.config and add
<connectionStrings>
<add name="XYZDatabase-TEST"
connectionString="Data Source=OTHERSERVER;Initial Catalog=XYZDatabase-TEST;Persist Security Info=True;User ID=sa;Password=password" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes"
xdt:Locator="Match(name)"/>
</connectionStrings>
Now, everytime you will publish you application to deployment server using Release configuration it will use the connection string from web.release.config
Note that this transformation will not work locally when you debug. You must publish to run the web.config transformation.

Related

connection string to Postgres

I am new to Postgres and I need help with connection string.
My app using Entity Framework. I have this connectionString to MSSQL Server:
<connectionStrings>
<add name="DBContext" connectionString="Data Source=localhost;Initial Catalog=DB;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
to my project i download a npgsql package (http://pgfoundry.org/projects/npgsql/) a need to help with editing connection string to Postgres database.
How to set providerName to npgsql?
Thank for help
"don't hard code your providers" discusses this in detail.
It shows a setting like what you want:
<add name="blah" providerName="Npgsql"
connectionString="Server=127.0.0.1;Port=5432;Database=myDataBase;
User Id=myUsername;Password=myPassword;"/>
but then explains why you should not do this, you should read the settings from your application's configuration file at runtime instead.

Using a separate file to maintain the connection string for entity framework

I have my connection string currently in my web.config file.
Is it possible to place it in a separate file and point entity framework to it.
I found the answer here Separate ConnectionStrings and mailSettings from web.config? Possible?:
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
With file connections.config containing
<connectionStrings>
<add name="name" connectionString="conn_string" providerName="System.Data.SqlClient" />
<add name="name2" connectionString="conn_string2" providerName="System.Data.SqlClient" />
</connectionStrings>
In case anyone stumbles upon this question. You can put the connection strings in a separate config file using configSource but DONT expect the EF designer to work happily with it.
Every time to go to edit the edmx and 'Update from Database' it will ask for a new connection string and then always want to save it back to the web.config. Not ideal and for me not workable. This is the case in EF6 and previous.

The connection string 'MyConnection' in the application's configuration file does not contain the required providerName attribute."

I use Entity Framework Code First,
My connection string is in a configuration file:
<connectionStrings>
<clear/>
<add name="ApplicationServices" connectionString="Data Source=PC-X;Initial Catalog=MYdb;Integrated Security=True"/>
</connectionStrings>
When I try to access the data (something that should create the DB) is falling with the following error:
The connection string 'ApplicationServices' in the application's
configuration file does not contain the required providerName
attribute."
What am I missing?
You're missing the following piece of code after the connectionString attribute (assuming that you're using SQL):
providerName="System.Data.SqlClient"
Sometime in the future. the complete code
<add name="YouContext" connectionString="Integrated Security=True;Persist Security Info=False;Initial Catalog=YourDatabaseName;Data Source=YourPCName;" providerName="System.Data.SqlClient"/>
Go down in your web.config until you reach the providers tag.
For instance, here's my providers statement:
<providers><provider invariantName="System.Data.SqlClient" ... /></providers>
you should add this System.Data.SqlClient as a provider name in your connection string
so your connection string should look like this:
<connectionStrings>
<add name="ApplicationServices" providerName="System.Data.SqlClient" connectionString="Data Source=PC-X;Initial Catalog=MYdb;Integrated Security=True"/>
</connectionStrings>
In my case the problem was with an incorrect StartUp project target. In the PM console the target migration assembly project was correct.
I have a multiproject solution and the target was on some web-service project.
So I changed the StartUp to the main WebSite project and the migration have complited without errors.

Entity Framework Code First Azure connection

I am using Entity Framework Code First 4.3 + Azure and having difficulties connecting to the database. The error I get is the following (on the first query):
Keyword not supported: 'server'.
I have the following connection set up in my Web.config
<configSections>
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<connectionStrings>
<add name="TestDBContext"
connectionString="Server=tcp:[SUBSCR].database.windows.net,1433;Database=[MyDB];User ID=[user];Password=[pass];Trusted_Connection=False;Encrypt=True;PersistSecurityInfo=True"
providerName="System.Data.EntityClient" />
</connectionStrings>
My DbContext implementing class uses the connection string's name:
public class MyContext : DbContext, IMyContext
{
public MyContext()
: base("TestDBContext")
{
Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = true;
}
Can you tell what is going on?
I just had the same problem.
You're missing all the metadata in the connection string that Entity Framework requires. The connection string provided by SQL Azure needs to inserted within the provider connection string parameter of EF's connection string.
<add name="MyConnectionString" connectionString="metadata=res://*/Model.Model.csdl|res://*/Model.Model.ssdl|res://*/Model.Model.msl;provider=System.Data.SqlClient;provider connection string="[PUT SQL AZURE CONN STRING HERE]"" providerName="System.Data.EntityClient" />
You'll need to use the metadata from your own project. I pulled that metadata from an EF project generating from an existing database.
I had the same problem. I solved, putting in the web.config this connectionstring:
<add name="eManagerTurModelConnection" connectionString="metadata=res://*/ORM.eManagerFinanceModel.csdl|res://*/ORM.eManagerFinanceModel.ssdl|res://*/ORM.eManagerFinanceModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=<server>.database.windows.net;Initial Catalog=eManagerTur;Integrated Security=False;User ID=<user>;Password=<Password>;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
And after I removed the connectionstring of my website, worked, because it was not getting the connection string that I added in my web.config.
English bad... =)
The provider should be providerName="System.Data.SqlClient"
I connected to Azure from VS and then looked at the properties and set my connection string and provider name.
<add name="context" connectionString="Data Source=myServer,myPort;Initial Catalog=myDBName;Persist Security Info=True;User ID=myUserName;Password=myPassword;" providerName="System.Data.SqlClient"/>
I was then able to run update-database with no issues.
i tried like this, it may help you. may be 1433 is making problem, is it port no ? or what? . try like this.
check this link Windows Azure with Sql
<add name="dbContext" connectionString="Server=tcp:xxxxxxxx.database.windows.net;Database=xxxxxxxx;User ID=xxxxxxx#xxxxxxxxx;Password=xxxxxxxxxx;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.EntityClient" />
Try this:
Data Source=tcp:YOUR-DATABASE-HERE.database.windows.net,1433;
Database=GolfRounds;
User ID=YOUR-USERNAME#YOUR-SERVER; Password=YOUR-PASSWORD; Trusted_Connection=False; Encrypt=True;
There is also an MSDN article at http://msdn.microsoft.com/en-us/library/windowsazure/ff951633.aspx that may be helpful.
I had a similar problem where I did not have access to the metadata, in this case you need to use System.Data.SqlClient as the provider. You will also need to add MultipleActiveResultSets=True to your connection string

Is it possible to reference a connection string in another connection string?

We have a project with multiple DLLs. In each DLL, we connect to a database - always the same one for a client.
As a result, we now have 3 near-identical connection strings : one for our web site, one for ado.net, and one for telerik reporting :
<add name="BDConnectionString" connectionString="Data Source=localhost;Initial Catalog=DATABASE;Persist Security Info=True;User ID=USER;Password=PASSWORD; MultipleActiveResultSets=True;Pooling=True;Max Pool Size=500;" providerName="System.Data.SqlClient" />
<add name="CMS.Reporting.My.MySettings.BDConnectionString" connectionString="Data Source=localhost;Initial Catalog=DATABASE;Persist Security Info=True;User ID=USER;Password=PASSWORD" providerName="System.Data.SqlClient" />
<add name="KOPWebEntities" connectionString="metadata=res://*/Data.web.csdl|res://*/Data.web.ssdl|res://*/Data.web.msl;provider=System.Data.SqlClient;provider connection string="Data Source=localhost;Initial Catalog=DATABASE;Persist Security Info=True;User ID=USER;Password=PASSWORD;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
Now it's still manageable, but in the future, we will have more ADO.Net Entity Framework connections, so maybe 10 connection strings by client ?
Is there a way to say, "ok, for this connection string, use the value coming from here" instead of duplicating it ? At least for ADO.Net ?
Or is there a better way ?
Thanks
I noticed you mentioned Entity Framework. It is possible to create entity contexts with different SQL connections than the one defined in the application/web config. (I'd have to dig up the code). However, as far as the more general "have a connection string that actually references another connection string" question, I'd say, if you don't have the source, tough luck.
It'd probably be better if your DLLs didn't reference app/web config settings and, for the classes defined in there, you could pass in either the, the "application key" which has an associated value referencing a connection string, the SQL connection string itself, a SqlConnection instance, or some SqlConnection-Factory-Thing you make up.