Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code First Entity Framework - change connection string

How do I change the connection string in a code first entity framework/MVC application? I'm trying to transfer it to a live site, but it overlooks web config values and still references my local version of the database.

Here is the connection string section of my web.config:

<add name="MembershipConnectionString" connectionString="Data Source=192.168.1.43;Initial Catalog=Website.Models.IntranetApplication;User Id=[UserName];Password=[Password];timeout=30" />
<add name="WebsiteConnectionString" connectionString="Data Source=192.168.1.43;Initial Catalog=Website.Models.IntranetApplication;User Id=[UserName];Password=[Password];timeout=30" />
<add name="Entities" connectionString="metadata=res://*/Models.IntranetModel.csdl|res://*/Models.IntranetModel.ssdl|res://*/Models.IntranetModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=192.168.1.43;Initial Catalog=Website.Models.IntranetApplication;User Id=[UserName];Password=[Password];MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

I'm not sure if the Entities string has any relevance, as I used Code First entity framework, and I think that only appeared when I tried to create an edmx file (although I ended up just deleting it). The Entities connection string has sat commented out so I don't think it's used.

I want entity framework to read the "WebsiteConnectionString", but it seems to want to use the local connection string, but I can't even see where that is set. How do I change it?

like image 689
e-on Avatar asked Aug 10 '11 13:08

e-on


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 connection string in Entity Framework?

A connection string contains initialization information that is passed as a parameter from a data provider to a data source. The syntax depends on the data provider, and the connection string is parsed during the attempt to open a connection.


1 Answers

The connection string or its name can be passed to constructor of DbContext. If you are using default constructor it searches for the connection string with the same name as the name of your derived context class and if it doesn't find it, it uses this one:

Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True

With database name same like your context class.

like image 93
Ladislav Mrnka Avatar answered Sep 20 '22 07:09

Ladislav Mrnka