Last post Sep 06, 2014 06:08 PM by Bobman
Sep 06, 2014 08:43 AM|Bobman|LINK
I've moved to VS 2013 and am creating an MVC/Web Forms application. I didn't know about EF defaulting the database to LocalDB and it has been driving me mad. The connection string I supply in web.config and used for the DBContext is ignored. I'm looking
to connect to a database on a web server and have the ip address for that.
I read that I should just change the data connection under Options->Database Tools->Data Connections. I've tried that but could be getting the syntax wrong (what actually goes into the box for "SQL Server Instance Name (blank for default)")? And if it
worked would I have to change the setting for every new project?
Thanks in advance.
Sep 06, 2014 08:57 AM|jsiahaan|LINK
I read that I should just change the data connection under Options->Database Tools->Data Connections. I've tried that but could be getting the syntax wrong (what actually goes into the box for "SQL Server Instance Name (blank for default)")? And if it worked
would I have to change the setting for every new project?
This "Data Connections" is needed between your Visual Studio and SQL Server. You need to change between your project and SQL Server. To change your connectionString on your Web.config at the root project folder, look at the following example:
<add name="PersonEntityDbContext" connectionString="metadata=res://*/PersonEntityModel.csdl|res://*/PersonEntityModel.ssdl|res://*/PersonEntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=HUMANIT2;initial catalog=TunaCoRe;persist security info=True;user id=huma;password=********;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add connectionString="Server=humanit2;Database=Ytitnedi;User ID=huma;Password=********" name="DefaultConnection" providerName="System.Data.SqlClient" />
Change the password based on your SQL server.
Hope this can help
Sep 06, 2014 09:04 AM|Bobman|LINK
Thanks for the quick reply. Here is the connection string I am using:
It's typical of a connection string I've been using for years. Has it now changed? How would this fit into the syntax you have shown?
Sep 06, 2014 09:16 AM|jsiahaan|LINK
That syntax is automatically generated when I create MVC template or Web API template, and my own connectionString is generated when I create .edmx ADO.Net.
Then I use this syntax any time I need to change SQL server by changing the computer SQL Server name. In your case change to IP address.
I gave you two samples, you may try one by one. By the way I did not see any error on your connectionString. In this case, try to create a similar project (temporary project) only to have connectionString syntax to fit to your project. This way I do to have
the right syntax.
Sep 06, 2014 02:23 PM|Bobman|LINK
jsiahaan - thanks for your help and patience. Unfortunately, I'm still not getting it. My connection seems to get completely ignored and EF just goes with the default LocalDb. Is there a way to turn the default off?
Sep 06, 2014 06:08 PM|Bobman|LINK
Sorted. The connection string was fine. It was the way the context was coded.
Previously the class would have been named the same as the connection string and would have inherited from DbContext. Now the constructor from the class that inherits from DbContext passes the connection string name to the base class. This might help someone:
public class mydata_Context : DbContext
public mydata_Context () : base("my_conn_string") // from web.config
Database.SetInitializer<mydata_Context >(new CreateDatabaseIfNotExists<mydata_Context >());