I created an MVC4 solution in VS 2012 with EF5, and right now I am trying to get the database to be created Code First. In the Create method of the controller I added db.Database.CreateIfNotExists();
On my machine, I also have SQL Server 2012 running, and I would like the db to be created there instead of in some SQL Express engine or other lightweight solution.
I have tried several approaches to making a correct connection string, but all has failed, and now I have no idea as to how it should look. This is how it looks now, but nothing gets created, and all i get from the CreateIfNotExists is an error:
InnerException {"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"} System.Exception {System.Data.SqlClient.SqlException}
This is what my connection string looks like:
<add name="Tool.Models.Context" connectionString="Server=MSSQLSERVER;Initial Catalog=Tool.models.ToolDb.mdf;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
EDIT:
After struggling with this issue for almost a day, I resolved it by substituting localhost for MSSQLSERVER (Which still is the name of the instance if I look at the Configuration Manager). Thanks for the comments though. Made me think.
localhost is the local server, not the instance, you appear to be using the default instance (called mssqlserver) and so don't need to specify the instance name, just the server name, hence why substituting MSSQLSERVER with localhost in your connection string works.
You could also use the actual name of the server in here, e.g. SERVER-01 (or whatever the actual name of your particular DB server is) and it would have the same effect.
If however you had installed a named instance on the machine, you would have to specify both the server name and the instance, e.g. localhost\instancename or server-01\instancename
Hope this helps explain why putting in localhost works.
That's right...
By default, as most may already be aware, Visual Studio 2012 - ASP.NET MVC 4 templates
will define a Default connectionString that points to a (LocalDb)v11.0 in Web.config.
In the case of Visual Studio 2010 ASP.Net MVC 4 project templates, it pointed to SQLExpress...
In my case I prefer pointing the connectionString to a specific SQL Server 2012 I use for dev.
So let's say I open SQL Management Studio and see SQLServer2012-01 name in a droplist where I want the database + tables to be created,
well that's the name I'd normally use...
connectionString="Server=SQLServer2012-01; InitialCatalog=..."
All you have to do afterwards is straightforward - follow the steps in defining
-Enable-Migrations
-[fill your Seed method with content - if needed...]
-Add-Migration MyFirstMigration [or any migration name you want]
-Update-Database -Verbose
and you should see results updated in Server Explorer Window.
Hope it helps
Related
When I am in the Dataset Designer and I ask to Preview Data on a Fill query all is well and data is displayed. When I try to use the TableAdapter.Fill method inside a program I get an exception for Error 26. This code was copied down from Team Foundation Server and the Connection Strings were changed to reflect the new server. All works fine on the old server.
I wrote another short program on the new server, added the same SQL Server Datasource and all is well, I can fill a Table Adapter.
Visual Studio Professional 2013, Sql Server Express 2008 R2, Sql Authentication in both programs.
I should also add that the code works fine in the original environment. The TableAdapter in the Designer displays the data from the database without a problem. It's only when I use an instance of that TableAdapter in my code that the Fill method throws an exception. I also get the same Exception when I use the GetData Method.
Any ideas?
SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified
This error is related to Sql Server Connection string, check your connection string specified in web.config
1) Make sure your server name is correct, e.g., no typo on the name.
2) Make sure your instance name is correct and there is actually such an instance on your target machine. [Update: Some application converts \ to . If you are not sure about your application, please try both Server\Instance and Server\Instance in your connection string]
3) Make sure the server machine is reachable, e.g, DNS can be resolve correctly, you are able to ping the server (not always true).
4) Make sure SQL Browser service is running on the server.
5) If firewall is enabled on the server, you need to put sqlbrowser.exe and/or UDP port 1434 into exception.
I have a project created while running 2008r2 express and connecting to an instance named "Sqlexpress". I have recently uninstalled Sql server 2008r2 express and started using the recommended LocalDB. All of my projects updated the database successfully and connect flawlessly with the exception of one, my largest project.
The database updated perfectly but now i get a Error 26 - Network or instance related error each time i try to login in. (iterating through a dataset created in the dataset desinger.
Here is my connection string:
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True
I can see the connection in Server Explorer and view table data. Any idea why this wouldn't work at runtime on this one project?
I solved this by opening Dataset.Designer.vb in notepad++ and replacing all instances of ".\SQLEXPRESS" with "(LocalDB\v11.0". I also had to set each instance of "User Instance=True" to False.
I solved the same by replacing the "(localdb)\v11.0" with applicable "MyServerName\SQLServerInstanceName" in connection string. During the installation of SQL Server Express 2014, I chose to keep SQLExpress as instance name.
I'm trying to get a very simply EF Code First example running but ran into the above problem. I've followed the advice here ( How to configure ProviderManifestToken for EF Code First ) but to no avail. I finally managed to get EF to create my database and tables by passing the actual connection string to the DBContext instead of the connection string name I had defined.
Here is how I defined by connection string initially in app.config
<connectionStrings>
<add name="ProductContext"
connectionString="integrated security=SSPI;
data source=MYMACHINE;
persist security info=False;
initial catalog=Product"
providerName="System.Data.SqlClient" />
</connectionStrings>
The name "ProductContext" matches the class ProductContext and the database Product does not exist.
Following the advice on a previous thread I passed the connection string name to ProductContext and the base DBContext cstor. This did not work either.
Finally, I passed the connection string above instead and everything worked, the db was created and the tables.
I'm using SQL Server 2008 and EF 4.1. Any ideas as to what I'm doing wrong?
Thanks,
Ken
Update
The application is a WPF application, not a web application. I get the same exception after I remove the connectionstring from app.config:
"A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)"
The problem with answering this question is that the culprit could be more than one issue.
That said, I see a couple things to check:
Your Data Source name looks to be invalid. You need to declare the host name and the instance name. Example: .\SQLEXPRESS (notice the . dot) or MyServerHostName\MySqlInstanceName.
Verify you have a valid data source by using SQL Server Management Studio and trying it there first.
Permissions. You are using integrated security and so the security context of the application making the database connection must have proper rights. Things to check:
Using SQL Server Mgmt Studio, connect to your database. Select your database in the left pane (Object Explorer) --> Databases --> Select your db --> Security --> Users. Make sure you can verify the account being used to run the app has either been granted rights explicitly or can inherit them by being a member of the groups.
When I had this error, my problem proved to be that I had mixed Integrated Security=SSPI with a "Username =" and "Password =" ...not realizing that I needed Integrated Security=false; my user and password parameters were being ignored.
Not sure about this one: I did read some people suggest (at least for troubleshooting) to set Persist Security Info=true.
I had similar problem on EF6 VS 2013, the problem got solved, when I choose 'Save Password' on Entity Framework - Reverse Engineer Code First - Connection Properties Dialog. Hope this helps someone.
After resolving issues with RIA installation here, I'm still getting this following error. Not much special - I tried to create a new screen based off of data sources from an existing database in a full SQL 2005 instance
An error occurred while establishing a
connection to SQL Server instance
'.\SQLEXPRESS'. A network-related or
instance-specific error occurred while
establishing a connection to SQL
Server. The server was not found or
was not accessible. Verify that the
instance name is correct and that SQL
Server is configured to allow remote
connections. (provider: SQL Network
Interfaces, error: 26 - Error Locating
Server/Instance Specified)
I've tried to correct the connection string in the, I guess, autogen'd config file. Even though Visual Studio doesn't seem to think that the file is overwritten on rebuild, the error continues to appear.
How can I use LightSwitch with full SQL 2005 rather than SQL Express?
It seems when working Locally, LightSwitch uses SQL Server Express. It puts it's Membership, Roles, and other authentication information here. I don't know any way to get around this locally, but when you go to "Publish" your project, you're given the ability to use a different database/server. You can also use whatever server/database you'd like for your data sources as well.
Hopefully, before V1, they'll allow you to change that. But I haven't found a way yet, without the config being overwritten constantly.
You can't use it with non Express version during development when you want to use LightSwitch to create your data model. If you deploy your project, as mentioned, you can use the created database with full version and change the connection string.
Although I would be cautious using SQL Server 2005 because LightSwitch is using datetime2 which is new in 2008. (maybe some more 2008 specifics as well)
http://lajak.wordpress.com/2011/10/04/lightswitch-change-default-database-instance/
Here, they are telling how to change instance name both in project level and global level.
Sorry if this is the most seen question on the web, but this is my turn. I am trying to publish my asp.net mvc app on IIS 7 under MS Sql Server 2008. I am on a Windows Server 2008 virtual machine. I get the following classical error:
A network-related or instance-specific
error occurred while establishing a
connection to SQL Server. The server
was not found or was not accessible.
Verify that the instance name is
correct and that SQL Server is
configured to allow remote
connections. (provider: SQL Network
Interfaces, error: 26 - Error Locating
Server/Instance Specified)
Under SQLServer, Allow remote connections is checked. My connection string is:
Data Source=.\MSSQLSERVER;Initial Catalog=mydbname;User Id=sa;Password=mypassword
I also tried with no username/password and "Integrated Security=true". There is only one instance of SQLServer installed.
I tried to access my web page locally and remotely. There is no active firewall on the virtual machine.
Make sure you have TCP/IP set up as a transport in your SQL Server configuration tool.
Thanks guys for the try. I found the solution and it is related to an info that I forgot to give. I hope it can help someone as new as me on these things.
I use NHibernate, and the connection string is actually in the nhibernate.cfg.xml file. The one in the web.config file is actually used by my various providers (users/roles). I fixed that by removing the connection string from the NH config file. I now retrieve it with:
string connectionString = ConfigurationManager.
ConnectionStrings["myConnectString"].ConnectionString;
and I set it in NH with:
Configuration cfg = new Configuration();
cfg.Configure(cfgFile);
cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString, connectionString);
Now I get:
Cannot open database "mydb"
requested by the login. The login
failed. Login failed for user 'NT
AUTHORITY\NETWORK SERVICE'.
But this is another story, for another question if I can't find the answer.
PS: I had to use "." as the server name otherwise .\MSSQLSERVER was producing a new error "invalid connection string". Thx Ian and Jared for the tip.
is that the actual data source line from the web config?
If so then it's should be in quotes of course, for safety add a ; on the end and check that is the actual name of your instance, you can check windows services for your instance name.
try substituting localhost instead of . and can you connect from sql server management studio using the credentials in your connection string?
As mentioned by Robert, try:
ConnectionString="Data Source=(LOCAL)\MSSQLSERVER;Initial Catalog=mydbname;User ID=sa;Password=mypassword"
I'm not sure if connection strings are case sensitve, but I notice that you have 'Id' instead of 'ID'.
Edit:
Am not sure if you need \MSSQLSERVER?
are you sure it's a named instance of SQL?
try
Data Source=.;Initial Catalog=mydbname;User Id=sa;Password=mypassword
UPDATE:
from this site
did you try the following:
Make sure the server machine is reachable, e.g, DNS can be resolve correctly, you are able to ping the server (not always true).
Make sure SQL Browser service is running on the server.
If firewall is enabled on the server, you need to put sqlbrowser.exe and/or UDP port 1434 into exception.
Well I am facing the same above issue since morning (past 8 hours) did lots of stuff like create a Domain Name, setup a new application pool identity but nothing worked :(
I just made a small changes in web.config file for connection string that is:
Integrated Security=False instead of True... and now it is working perfectly