Dll with Entity Framework unable to connect to database - c#

I have a separate dll that handles connections to the database and thus holds the models and the api context. the app.config 'acts' like the web.config and holds the connection details However I keep getting the following 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)
So I copied the connection string from another more traditional mvc web.config file and the error still occurs.
Oddly, the following works:
update-database -verbose
My app.config has the following connection string:
<add name="DefaultConnection"
connectionString="data source=(LocalDb)\v11.0;initial catalog=APIEntities-01;persist security info=True; Integrated Security=SSPI;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
and the dbContext points to it
public ApiContext() : base("DefaultConnection")
{
}
As I say, this works in a traditional MVC web.config, but using a dll that references Entity Framework and such like it seems to not be able to locate the database. Any suggestions?
EF 6

I suppose that the connection string because the "data source=(LocalDb)\v11.0;initial catalog=APIEntities-01" is a path local to your PC. You must provide the real SQL Server that you need to use. See this:
Change default location of LocalDb

Some times its hapen when sql server failed to open. Then open sql server configuration and then select (sql server Services) see on state which are stopped or manual...right click on then and make start...then the problem will be solved.

Related

Entitiy exception was unhandled by user code

I was given a certain project (asp.net project) that was running on a VM. I copied that project to my local machine.(A db is connected to visual studio using entity framework) The sql server that i have has the follow name.
"BASIT-GILANI\MSSQLSERVER1"
and i use windows authentication to login to sql server mgmt studio.
I am continually getting this error
Entitiy exception was unhandled by user code An exception of type
'System.Data.Entity.Core.EntityException' occurred in mscorlib.dll but
was not handled in user code
Additional information: The underlying provider failed on Open.
I am also giving a piece of code from my web.config file
<connectionStrings>
<add name="IntegrationDBEntities" connectionString="metadata=res://*/IntegrationDb.csdl|res://*/IntegrationDb.ssdl|res://*/IntegrationDb.msl;provider=System.Data.SqlClient;provider
connection string="data source=BASIT-GILANI\MSSQLSERVER1;initial catalog=ODSDB;MultipleActiveResultSets=True;App=EntityFramework;Connection Timeout=160"" providerName="System.Data.EntityClient" />
</connectionStrings>
Does anyone have any idea of how to solve this issue?
Do you have sufficient privilege to access the database ?
Have you already try to connect from a different SQL client like SQL Server Management with the same credentials ?

Getting 'connection to SQL server' error while installing a .exe file of my c# project on client's computer

I have created software in c#.net which has some database into it. After creating a .exe file it works fine in my system but after installing that
application in another system it gives following error.
Error: A network-related or instant 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 connection. (provider: SQL Network Interfaces, error: 26 - Error locating server/instance specified).
I have tried installing SQL Server and .net framework in client's machine but still, it's not working.
Do I need to install anything else on the client's pc to run my application?
Check the connection string to the database. The connection string should point to the SQL database the Client has access to. Most of the time this is not the same as the one you are using while developing.
Change connection string of localhost to IP address of Database PC.
You are going to have to add a connectionstring to your App.config file.
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=PRANOYPC\SQLEXPRESS; Initial Catalog=DatabaseName; Integrated Security=True;" providerName="System.Data.SqlClient"
</connectionStrings>
If you are using Windows authentication you can leave Integrated Security = True if not you will have to provide the authentication for the Database.
Also you have to make sure that the Client has access to the database that you are trying to access.

Publishing ASP.net website With LocalDB

I'm trying to publish my website on a different computer that is on my home network. I have set up IIS and SQL Server 2016 Express on that computer. Same version on my development laptop. When I run the site I get the below error. I think the problem might be that I'm trying to us LocalDB. I'm often traveling and in areas where I don't have internet connectivity. This is why I thought it would be best to use localDB while developing. Once I get home I then publish to my Server.
Originally the LocalDB was created on my computer in a folder that was far from my project. So what I did was move it to the app_data so that it would be attached when I publish.
What I tried:
Reinstalled Microsoft SQL Server 2016 (made sure LocalDB was installed)
Ensured the database was in fact in App_Data folder on server
Changed connection string to look for database in app_data
Any suggestion would be great.
Connection string:
<connectionStrings
<add name="IgorsSellersToolBoxDB"
connectionString="metadata=res://*/Models.ModelDB.csdl|res://*/Models.ModelDB.ssdl|res://*/Models.ModelDB.msl;provider=System.Data.SqlClient;provider connection string="data source= LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\IgorsSellersToolBoxDB.mdf;integrated security=True;multipleactiveresultsets=True;connect timeout=30;application name=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Error:
Server error in '/WebToolbox' Application.
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.
)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: 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.)
Source error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.`
As Steve mentioned in the comments, I have confirmed that using localDB in production is frowned upon.
Solution:
Use a SQL Server on my development machine and a separate one on my production server. All I have to do is have separate connection strings in my Web.Config.
In your web.config, you can expand the file in the solution explorer, and you will see two files:
Web.Debug.Config
Web.Release.Config
There is a great answer here that explains it very well -->Using different Web.config in development and production environment

Using LocalDB with Visual Studio

I have Visual Studio 2015 installed and I created a sample application from some Microsoft tutorial.
This is the connection string I got in web.config:
<add name="DefaultConnection"
connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-GeekQuiz-20160203041206.mdf;Initial Catalog=aspnet-GeekQuiz-20160203041206;Integrated Security=True"
providerName="System.Data.SqlClient" />
But it doesn't work, this is the error I got:
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.
Should I install SQL Server Express separately or I got it along with Visual Studio?
I usually use the next trick to set the correct connectionString in the Web.config:
In Visual Studio, go to the “SQL Server Object Explorer” label, right-click on the Database you wish to use and click on “Properties”. Then get the string in the “Connection string” field.
Try giving permissions to SQL server on the directory of App_Data. Go to properties->Security->Edit->Add and add the sql server instance name. It is usually NT Service\MSSQL$InstanceName. You can find instance name from sql server configuration manager-> Under SQL Server Network Configuration. If this doesn't help, then try adding PC Name\Users. It should work after that

Deployment to Azure from Jenkins not working because of "A network-related or instance-specific error occurred" (Error 26)

A friend of mine setup a Jenkins build server with automatic deployment to Azure. The website used to work fine after deployment, but after we branched our solution and then merged it again (after changing from .aspx to razor) the website displays the following error (locally everything works fine):
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)
Unfortunately we (the two that are left behind) cannot ask our friend how to solve this since he is on a vacation.
After searching on the internet we think it has something to do with our connection string that might be changed. Locally we can connect to the remote Azure database via the "Connect to Database" option in the Visual Studio Server Explorer. When we right click this connection and copy the connection string into our web.config and/or publish profile we still get the same error message on the website.
Our connection string in the web.config looks like this:
<add name="DefaultConnection" connectionString="Data
Source=*******.database.windows.net;Initial Catalog=blablaAszH;Persist
Security Info=True;User ID=testserver#blabla;Password=ourpassword"
providerName="System.Data.SqlClient" />
I do not know what other relevant information I can post but if you ask for it I will place it here.
(The main goal is actually just to deploy the website. But we cannot do this via Visual Studio since only our friend knows the username and password and he's disconnected from the world. These are not the same as in the connection string since when we publish from Visual Studio it says the web deployment task failed because of a wrong username and password. So we need to publish via committing our project to SVN and Jenkins doing the deployment.)
Does anyone here know how to solve this issue or with what it might be related?

Categories