ASP .NET web api application database error after deployed to Azure - c#

I have an ASP .NET web api project that connects to MS SQL database using entity framework. The project works fine when ran locally.
I published the project on Azure, created a DB in cloud, also added the necessary tables in the DB. After the website is published, everything seems to be fine until I try to access the controller (Eg: mywebsite.azurewebsites.net/api/Groups), I get this error:
I have Enable Migration before publishing my project. When enabling migration, I got an error saying there are two DB: ApplicationDbContext, and myDB (which is where the tables are). So I enabled migration only on myDB. Does the error has anything to do with that?
EDIT:
Here is the connection string from Azure and web.config file:
Web.Config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=tcp:xxxxxxxxx.database.windows.net,1433;Initial Catalog=IMS;User Id=userName#xxxxxxxxx;Password=myPassword" providerName="System.Data.SqlClient" />
<add name="IMSEntities" connectionString="metadata=res://*/Models.IMSEntities.csdl|res://*/Models.IMSEntities.ssdl|res://*/Models.IMSEntities.msl;provider=System.Data.SqlClient;provider connection string="Data Source=tcp:xxxxxxxxx.database.windows.net,1433;Initial Catalog=IMS;User Id=userName#xxxxxxxxx;Password=myPassword"" providerName="System.Data.EntityClient" />
<add name="IMSEntities_DatabasePublish" connectionString="Data Source=tcp:xxxxxxxxx.database.windows.net,1433;Initial Catalog=IMS;User ID=userName#xxxxxxxxx;Password=myPassword" providerName="System.Data.SqlClient" />
</connectionStrings>
From Azure (All Settings >> Application Settings >> Connection Strings). The name of the connection string in Azure is DbConnectionString
Server=tcp:xxxxxxxxx.database.windows.net,1433;Initial Catalog=IMS;Persist Security Info=False;User ID=userName#xxxxxxxxx;Password=myPassword;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
Thank you for your help!!

Related

ConnectionString problem.Can not communicate with database after iis deployment

After publishing my locat asp.net website to IIS server, it can not communicate with database. I'm checking if the api are working properly with Postman. And here is the error.
<connectionStrings>
<add name="HsptlMgmnt" connectionString="server=amso1; database=hoptialfinaldb;Trusted_Connection=false;uid=tanvir;password=1234;" providerName="System.Data.SqlClient" />
</connectionStrings>

Installing DotNetNuke : Database Connection Failing

I am trying to setup DNN on my local machine but having following error.
Installing DotNetNuke
Upgrade Error: ERROR: Could not connect to database specified in connectionString for SqlDataProvider
I have a SQL Server 2014 Express installed with custom login created.
I can login and access to database with same credentials using SQL Management Studio.
In web.config file, I have following lines for Database connection:
<connectionStrings>
<add name="SiteSqlServer" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=<dbName>;User ID=<UID>;Password=<PWD>" providerName="System.Data.SqlClient" />
<add name="SiteSqlServerAcuity" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=<dbName>;User ID=<UID>;Password=<PWD>" providerName="System.Data.SqlClient" />
<add name="AcuityEntities" connectionString="metadata=res://*/DAO.Model.SalesAcuity.csdl|res://*/DAO.Model.SalesAcuity.ssdl|res://*/DAO.Model.SalesAcuity.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;Initial Catalog=<dbName>;Persist Security Info=True;User ID=<UID>;Password=<PWD>;MultipleActiveResultSets=True;Application Name=EntityFramework"" providerName="System.Data.EntityClient" />
and
<appSettings>
<add key="SiteSqlServer" value="Data Source=.\SQLEXPRESS;Initial Catalog=<dbName>;User ID=<UID>;Password=<PWD>"></add>
</appSettings>
I verified and changed connectionstrings in both sections as per so many references on internet but none of them worked.
Is there any other configuration needed? Or any change needed in web.config or Database configuration ?
THIS IS NOT A DFAULT DOTNETNUKE PACKAGE. ITS A CUSTOM PROJECT I AM TRYING TO SETUP.
There are a few things to consider, depending on the nature of the setup that you have.
Ensure that the Web Server can connect to the DB server. You may be able to connect to the server, but the web server itself cannot
Ensure that the user account being used has DBO rights, if DNN needs to upgrade etc it needs that level of permissions
This is more of a comment to #Mitchel Sellers answer, point 1. But it's a bit too much for a comment field.
Run the SQL Server Configuration Manager.
Go to "SQL Server Network Configuration".
Click "Protocols for <INSTANCE>".
In the right pane double click on "TCP/IP".
Make sure that "Enabled" is set to "Yes".
Go to the tab "IP Adresses".
For each IP, set the "TCP Port" to "1433", the "TCP Dynamic Port" to
"0" and "Active" and "Enabled" to "Yes".
With "IPALL" just set the port to "1433", leave the Dynamic as is.
Reboot or restart the SQL Server service.
With these steps I get a connection to the database from IIS using localhost
If you cannot find the Configuration Manager in the Start Menu, you can run it manually with C:\Windows\SysWOW64\mmc.exe /32 c:\Windows\SysWOW64\SQLServerManager12.msc

EF 6 "Unable to load the specified metadata resource" -- but I'm not using edmx

Sorry if this has been asked, but I've gone through all of the other posts I can find and everyone seems to be using an EDMX, and not a "production" database.
I get the following error:
System.Data.Entity.Core.MetadataException: Unable to load the specified metadata resource
when attempting to access my application. My connection string is as follows:
<add name="FormAssemblyEntities" connectionString="metadata=res://*/Models.Entities.FormAssembly.csdl|res://*/Models.Entities.FormAssembly.ssdl|res://*/Models.Entities.FormAssembly.msl;provider=System.Data.SqlClient;provider connection string="data source=server\instance;initial catalog=dbname;user id=userid;password=password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
This works perfectly if I am running it locally and it connects to the edmx with the following connection string, but as soon as I put it on our UAT server and point it to the database, I get that error.
<add name="FormAssemblyEntities" connectionString="Data Source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\FormAssembly.mdf;Initial Catalog=FormAssembly;Integrated Security=SSPI;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
I really have no idea where to start. The database appears up to date, the user has access, and the server has access to the SQL server.
I have also tried replacing the res://*/ with res://FormAssembly/ and res://FormAssembly.dll/, but the former results in the same error, and the latter says the dll can't be found (the DLL is in the bin directory).
Any help would be appreciated.
Thanks,
Travis
In my case, the metadata part of my connection string was malformed.
The following connection string worked in my case:
<connectionStrings>
<add name="NorthwindEntities" connectionString="metadata=res://*/Northwind.csdl|res://*/Northwind.ssdl|res://*/Northwind.msl;provider=System.Data.SqlClient;provider connection string="data source=(local);initial catalog=Northwind;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>

Login failed while connecting to database

I have created WPF application which has MDF file. When I added MDF file to my solutions folder It automatically created a connection string for me
<add name="Connection"
connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=C:\Users\Public\Desktop\MyDatabase.mdf;Integrated Security=True;"
providerName="System.Data.SqlClient" />
I Deployed my application using SQL Server Compact (as described here). But When I run my Application I am getting exception
Cannot open database "C:\Users\Public\Desktop\MyDatabase.mdf" requested by the login. The login failed.
Login failed for user 'MY-PC\NAME'.
I am using Windows Authentication for my SQL Server
change your connection string as follows:
<add name="Connection"
connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=C:\Users\Public\Desktop\MyDatabase.mdf;persist security info=True;Integrated Security=SSPI;"providerName="System.Data.SqlClient" />

How to embed database into final c# .exe file?

I have completed my project in c#. I have used SQL Server Compact database (.sdf file). Now, when I compile and run, it works fine. But, how will I be able to run the application in other computers without configuring the connection to database?
In your App.config file add the following after </configSections>:
<connectionStrings>
<add name="NameOfYourConnectionString" connectionString="Data Source=.; AttachDbFilename=|DataDirectory|\student.sdf;Initial Catalog=student; Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

Categories

Resources