ASP.NET MVC failure to create database [duplicate] - c#

This question already has answers here:
Cannot Connect to Server - A network-related or instance-specific error
(54 answers)
Closed 6 years ago.
I'm new to ASP.NET MVC and was just messing around with it and trying to see how it works. So I decided to make a query to the database accessed with the default ApplicationDbContext. Normally, if the database hadn't been instantiated before that, this query would make sure to first create the database and then make the query, right? Well, here's what I did:
// This is the default Index action in the Home controller:
public ActionResult Index()
{
var db = new ApplicationDbContext();
var usersCount = db.Users.Count();
return View();
}
I know that the query is pretty meaningless but the sole purpose of it was for it to generate the database. The thing is it that it doesn't. I get a System.Data.SqlClient.SqlException when trying to execute the query (second line of the method).
Even when checking the App_Data folder I see that the database isn't created. What could be the cause of this? Strange thing is that this works on my desktop computer, but not on my laptop. But I did notice that when I put an invalid Data Source(ex. Data Source=.) in the connection string I get the same exception on my desktop computer. So maybe something is happening with the LocalDb that's causing this behaviour.
The connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-DefaultProject-20160731061747.mdf;Initial Catalog=aspnet-DefaultProject-20160731061747;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
This is the full exception:
An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code
Additional information: 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: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)
I would be very grateful if someone can come up with a solution for this!
P.S: Everything works fine on my laptop if I change the Data Source to my SQL Server Express server name.

Sounds like your LocalDb instance isn't installed. This is a bit of a "dev attachment" version of SQL that's installed with certain versions of Visual Studio and might have been de-selected or just plain not included perhaps if the original install was older and has gone through upgrades, etc.
This post gives a decent explaination: How to install localdb separately?
Basically google for SqlLocalDb.msi and follow the instructions.
I generally prefer Express over localDb anyway, but that's up to you.
Also note that connection strings are considered "loose" information in the web.config for MVC stuff. That is, they tend to be specific to the machine, so if you're changing machines (or working on a team), your machine is likely to have a different connection string (or a local copy/partial of web.config).

Related

IIS seems to not being able to connect using Entity framework provider

I've being trying in vain to solve a problem on a newly created windows server with IIS hosting an ASP.NET application that was copied from another environment. The issue is that I cannot manage to connect to the database when the application tries to open a connection via entity framework.
If this is off topic for SO let me know so I can move to another channel, I thought it belonged here as seems specific to EF.
In detail, I have the following connection strings:
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=LOCALCOMPUTER\SQLEXPRESS;Initial Catalog=xxx;Persist Security Info=True;User ID=yyy;Password=zzz" providerName="System.Data.SqlClient"/>
<add name="MembershipConnection" connectionString="metadata=res://*/App_Code.Membership.Membership.csdl|res://*/App_Code.Membership.Membership.ssdl|res://*/App_Code.Membership.Membership.msl;provider=System.Data.SqlClient;provider connection string="Data Source=LOCALCOMPUTER\SQLEXPRESS;Initial Catalog=xxx;Persist Security Info=True;User ID=yyy;Password=zzz;MultipleActiveResultSets=True;Application Name=EntityFramework"" providerName="System.Data.EntityClient"/>
...
xxx, yyy, zzz of course have the proper values. LOCALCOMPUTER is the FDQN of the local computer, which is not on a domain
The first connection string is for the Microsoft login library, while the second uses entity framework from our code.
When I browse to the site, I can successfully access the login form, and if I try to insert a wrong password i get a message saying so, meaning the application managed to check the database using the first connection string, but when i insert a correct one, i land on an exception as soon as the code tries to create a context.
The underlying provider failed on Open.:
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: TCP Provider, error: 0 - The wait operation timed out.)
In the event viewer it tells me that the user has been authenticated via Authentication Type: Forms , which I assume it means the auth system (the one using ApplicationServices connection string) is working, the stack trace confirms that the error occours when I try to use EF in code, meaning the DB has been used succesfully to check user credentials
note that:
The web app works in other environments
I can connect to the sql server using the credentials via SSMS
I've assigned a local windows account to the app pool that can also access the database, and tried changing to Integrated Security=SSPI
Both .Net 3.5 and .Net 4.8 are installed
The database is on the local machine and has been restored from a dump of the other one, its users have been mapped, and to rule out issues i gave it ample authorisations with dbowner datawriter and datareader
I've been trying to fix this since a couple days checking similar issues on SO with no avail and I don't really know how to proceed, is there something I can do to troubleshoot the issue?
Apologies, I've found out the answer, and it's specific to the code base. Apparently the connection string was being ignored (despite being perfectly usable) in favour of data saved in a table.
Had to contact the previous developer to find out, guess it's a good example of how important is to document your code
I'll close the thread, thanks for all who tried to help

C# Update Connection String Visual Studio

I am having some connectivity issues with Visual Studio and SQL Server.
This is the connections string in my web.config file;
<connectionStrings>
<add name="WintipToysConnectionString" connectionString="Data
Source=localhost\sqlexpress;Initial Catalog=WINGTIPTOYS;Integrated
Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
According to my DB connection string found in Visual Studio, my connection string is;
Data Source=localhost\sqlexpress;Initial Catalog=WINGTIPTOYS;Integrated Security=True
Which seems to add up - but, I get this error when my program hits a certain line;
System.Data.SqlClient.SqlException: '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: 50
- Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
I am actually following the WingTipToys tutorial on the Microsoft website and I am struggling to get it to use the Database I attached to SSMS.
I can write queries inside VS which works fine. So I don't really understand why my program is having this error when running?
Thanks.
Admittedly haven't used SQLExpress in a looong time. It looks like the syntax you're using is for an "instance" - which is pointed out in the error message:
...Cannot create an automatic instance...
What you have does look like server\instance conn string syntax...
Connect to the "default" instance of SQL Express like so: Data Source=.\\SQLExpress
Refer to this:
SSE Connection string sample
Hth...

Service-Based Database Entity Framework Connection Error On released version

I am writing a Windows Forms application, where I want to save data inside the application. For the reason I used a serviced-based database with a .mdf database file, and I use Entity Framework.
So far everything is working fine until I released the file and opened it on another computer. The application opened and everything worked fine but when it comes to the database interaction, it throws a big error like this:
************ Exception Text ************
System.Data.Entity.Core.EntityException: The underlying provider failed on Open.
System.Data.SqlClient.SqlException: 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: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.)
Here a screenshot of the error: https://www.dropbox.com/s/gwban6ab97c6fya/22093543_10213995193571435_760919436_n.png?dl=0
In case you need the project its uploaded to here: https://www.dropbox.com/s/ubpc683ggtihh6k/usercontrol.zip?dl=0
I have tried in so many ways but nothing is working, same thing happens for the installer version as well.
Can anyone help me on this please?
Here are my connection strings:
<add name="DatabaseEntities"
connectionString="metadata=res://*/dbModel.csdl|res://*/dbModel.ssdl|res://*/dbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\Database.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="OPLCheque.Properties.Settings.DatabaseConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
I'm pretty sure this post would be flagged as duplicate since a similar question has been answered multiple times already, but here you go.
You are using LocalDB which means it cannot be shared since it runs on SQL Server Express.
If you simply want a LocalDB, see this answer to implement it after publishing your WinForms app.
Basically you want to publish your app with prerequisite components so you should publish those along your application.
If you want to use a single database to be shared between instances of your application, learn how to setup one using SQL Server here.
Afterwards, learn what connection string format you should use.
Sample to connect via IP Address:
connetionString="Data Source=IP_ADDRESS,PORT;
Network Library=DBMSSOCN;Initial Catalog=DatabaseName;
User ID=UserName;Password=Password"
See this for more.
If ever you plan on using a shared database, better make sure that the connection string that your database context should use is specified.
Config:
<add name="DefaultConnection" connectionString="" providerName="" />
Database Context:
public DbContext()
: base ("DefaultConnection")
{
}

Visual C# 2010: Unable to connect to XAMPP localhost

I am unable to connect to XAMPP localhost in my C# Windows form application.
My code is:
SqlConnection connection = null;
try
{
connection = new SqlConnection("user id=root;" +
"password=12345678;" + "server=localhost");
connection.Open();
}
catch (Exception exptn)
{
MessageBox.Show(exptn.ToString());
}
I made sure to include "using System.Data.SqlClient;" so that my code runs. The timeout error I get at runtime is:
"System.Data.SqlClient.SqlException (0x80131904): 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."
There was more to the error message, but it was uncopyable and too long to post here. I have tried to install MySql and the .NET MySql connector separately from XAMPP. I added MySql.Data as a reference in Solution Explorer (My research told me to add MySql.Data and MySql.Web, and MySql.Data was the only one listed in the options.). Another source told me to add a Microsoft ODBC Data Source in Server/Database explorer, but the option was not available in Database Explorer. I have made sure XAMPP is running and working properly in phpMyAdmin, and I am able to create tables and run queries there. Many sources also seem to have different opinions as to what is needed in the connection string.
I am completely at a loss as to how I am supposed to connect to localhost databases from another application on the same computer. Forgive me if I am overlooking something basic or if this is a noob question, but I know little about connecting to databases (this type of thing is precisely the reason why). Can anyone help me connect to XAMPP localhost from C#?
Install MySQL Connector from Oracle and then use class MySqlConnection.
Here is the download link -
http://www.mysql.com/products/connector/
Download the driver for ADO.Net and the documentation to use it is here -
http://dev.mysql.com/doc/connector-net/en/connector-net-introduction.html
SqlConnection is for MQ SQL server only and thus it is looking for a instance of MS SQL Server to connect to and it fails to connect since none exists. This explains the error message.

cannot deploy c# app with entity framework

i have finished a desktop c# project in which i make use of Entity framework 5 and SQL server 2008
in my machine it's working well, but i created a virtual machine and tried to deploy it there and I have problems.
I get this error : System.Data.EntityException: The underlying provider failed on Open. --->
System.Data.SqlClient.SqlException: 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.
I used installshield for the setup and I have installed sql express in the visrual machine, during the setup I create the database and all the tables.
my connection string is this :name="BussinessContainer" connectionString="metadata=.\Bussiness.csdl|.\Bussiness.ssdl|.\Bussiness.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=yingyangDB;User ID=username;Password=password;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"
Should I change the connection string?
I searched a lot before posting this, but nothing worked..any help?
SQL Express (normally) doesn't create as a "Default instance" - i.e. connecting by localhost isn't sufficient to connect to the local SQL express instance.
You may need to use localhost\SQLExpress to connect to the local SQL Express instance (the instance name may be different, but usually it is not.
Additionally, you specify an Initial Catalog in your connection string, which I assume is pointing at your new DB. However you say that you create the DB during the installation - so that DB may not actually exist in your Instance yet. That may be causing it to fail out. Try taking it out of the string and see if that lets you connect.

Categories