connection asp.net project to azure database fail - c#

Trying to connect ASP.Net code first project to Azure. At this stage it launching application online as project was published. I can browse pages, but when I try to create new dynamic pages it gives me an error. My project works fine locally.
I uploaded project using Azure SDK and used info from azure such as the server name and connection string. Here it is:
<connectionStrings>
<add name="PhClub" connectionString="Server=tcp:phclub.database.windows.net,1433;Initial Catalog=PhClub;Persist Security Info=False;User ID=myloginname for azure;Password=******;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" />
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-PhClub-20170304023453.mdf;Initial Catalog=aspnet-PhClub-20170304023453;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
However, there was one thing which cached my eye but i don't know what it means and how to fix it. In App_data folder i have two databases, but in the publish window I see three of them:
As you can see something is wrong with last one.
Here how it looks in preview window:
Looking for your advise since this is my first solo project. Thank you in advance!

Try adding providerName="System.Data.SqlClient" at the end of the PhClub connection string.

If we create <customErrors mode="Off"/> in our web.config file, then we could see the details error message. We could get more info about how to troubleshoot a web app in Azure App Service using Visual Studio
We could debug it with following 2 ways:
1.Make sure that the connectionstring for dbcontext is the azure sql connectionstring
2.Make sure that Azure sql could be accessed by azure service.(Allowed default). More detail info about azure sql firewall please refer this document.

Try to ignore the # symbol with \ before or double it like ##.
Because with azure # separates loginName#server.
An alternative is to create a new user/login in the database, which's username doesn't contain a # and use this user in the connection string.

Thanks for all answers. It seems that problem was in connection string login credentials, i deleted web app, server and database, recreated it again with new simple login name. Changed all of the login name and password in connection strings. This way i got rid of the error.

Related

Error with Connection String in MVC

I am trying to retrieve data from an SQL Server database table in my MVC application. I have the following connection string:
<add name="EmployeeContext" connectionString="Data Source=localhost;Initial Catalog=SampleHR;Integrated Security=True"
providerName="System.Data.SqlClient"/>
But this gives me the following error:
Cannot open database "SampleHR" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\DefaultAppPool'.
On this line in my ActionResult code:
Employee empPersonal = epcontext.Employees.Single(emp => emp.EmployeeID == eid);
I tried the following two connection string:
<!-- <add name="Employee_PersonalContext" connectionString ="server=MANASI-HP\MSSQLSERVER; database=SampleHR; integrated security=SSPI;"
providerName="System.Data.SqlClient"/> -->
<!-- <add name="Employee_PersonalContext" connectionString ="server=MANASI-HP\MSSQLSERVER; database=SampleHR; username=dbo; password=;"
providerName="System.Data.SqlClient"/> -->
But these two gave me the error: "The underlying provider failed to open."
I have SQL Server 2012, with Windows Authentication. I ran the query "select CURRENT_USER" and got the username "dbo". I have not set any password and I am using Windows Authentication. I am also running the application on IIS server.
What could be the problem? Please let me know if I have to add any code here.
Edit: I went to project properties and changed the server to SQL Server instead of IIS and it works with this connection string:
<add name="EmployeeContext" connectionString="Data Source=localhost;Initial Catalog=SampleHR;Integrated Security=True;User ID=IIS APPPOOL\DefaultAppPool;Password="
providerName="System.Data.SqlClient"/>
but not with this one:
<add name="MvcDemoConnectionString" connectionString="Server=MANASI-HP;Database=SampleHR;Integrated Security=true"
providerName="System.Data.SqlClient"/>
I am confused.. :(
Edit 2 (11th Nov): I did not make any changes to IIS Manager right before I started with the MVC application in .NET. Just followed the instructions in the tutorial. I found this page about IIS.
IIS Data Source Settings
Do I need to do any of these things before I start building my application on IIS?
Dec 5th 2015:
I followed the instructions on these two pages:
ASP.NET Web Deployment
and
Deploy Database with IIS
Still not working.
If you are running the app in IIS and your connection string uses integrated security, the user it is going to use is whatever the app pool is running as. You can either change the user the app pool is running as, or provide the username and password for the windows authentication user you want to use in your connection string (something like Data Source=localhost;Initial Catalog=SampleHR;Integrated Security=True;User ID=username;Password=password
Look like you have hosted application in IIS and allow anonymous access, and your connecting string accepting windows authenticated user.
Whenever you have this type of setting in place, I'll recommend to run IIS application pool under service account instead of AppPoolIdentity, and make sure your SQL Server allow service account user.
For testing go to Application Pool -> advance settings -> and change 'Identity' with you network/windows credential. if your account have access to sql server your problem will be solve for a moment.
Note: I'll not recommend to keep running your application under personal account. Because once you change your password you have to change the App pool Identity again.
If you don't change, each app pool has it's own identity. In your case, just add a new user to your database SampleHR with the name IIS APPPOOL\MyAppPool using SQL Management Studio. You find the users list in the "Security/Users" subnode of your database.
I just had to grant the user permissions in SSMS. I just went back to default settings in ISS, granted permission to the user in SSMS, and that was it.
Does anyone have any other solution?

Azure Mobile Services - Connection string not found after publishing

After I deployed my mobile service to Azure, calls to the service fails because of this error:
No connection string named 'ApplicationEntities' could be found in the application config file.
The error only occurs on Azure side. When I test the service locally, the connection to the remote database works without a problem.
I separated my solution into several projects:
Web Api
Business Logic
Data Access (contains the DbContext, database first)
Common (contains the entities generated by EF)
As I always do, I copied the connection string generated in my app.config of the DataAccess Assembly into the connectionStrings-Element of my web.config (Web Api project).
<connectionStrings>
<add name="ApplicationEntities" connectionString="<the connection string>" providerName="System.Data.EntityClient" />
</connectionStrings>
In the web deploy settings, I selected the connection string for "ApplicationEntities". I tested it with and without the option "Use this connection string at runtime (update destination web.config). I always get the same error.
Then I got curious and logged the connection strings available via the ConfigurationManager with something like this:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ConfigurationManager.ConnectionStrings.Count; i++)
{
sb.AppendLine(ConfigurationManager.ConnectionStrings[i].ConnectionString);
}
trace.Info(sb.ToString());
I got two connection strings:
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
Data Source=theserver.database.windows.net;Initial Catalog=thedb_db;User ID=theuser;Password=thepassword;Asynchronous Processing=True;TrustServerCertificate=False;
The username and password is strangely different from the username and password stated in the management portal.
Also the EF-Metadata information get lost.
I get this connection strings when testing locally:
data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
metadata=res://*/DataContext.ApplicationEntities.csdl|res://*/DataContext.ApplicationEntities.ssdl|res://*/DataContext.ApplicationEntities.msl;provider=System.Data.SqlClient;provider connection string=\"data source=theserveraddress,1433;initial catalog=thedb_db;persist security info=True;user id=theusername;password=thepassword;MultipleActiveResultSets=True;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;App=EntityFramework\"
I expect to get the same connection strings when running on Azure. Any idea what's going wrong?
Update:
I just went for remote debugging the service (see here). As I opened the downloaded publish-profile in my text editor, I discovered that there is an attribute SQLServerDBConnectionString - containing the connection string that always gets forced when deployed - with the same username and password I'm not aware of that it exists.
I tried to overrwite this connection string but it did not help. That connection remains the active.
Update 2 / May 29th 2014:
Seems that in the meantime the error has been fixed by the azure mobile team. Still running the same code using the database first approach and it's working now. Thanks a lot!
I have myself tried using DB first approach with dot net backend mobile services & ran into same sort of problems as you. Digging around further, following is my observation :
The azure mobile service with dot net backend must use code first approach only. This generates a specialised connection string with metadata (starting with res:)
The name of the connection string has to be the same as it is when you download the default sample todo app I.e. Ms_TableConnectionString.
There is an option in azure websites to select custom SQL provider to help make a custom connection string, this option, however, is not available for dot net backend mobile service in Azure Management portal.
P.s. Posting as answer because I don't have a enough points to comment..
Just wanted to give an update that I have been looking at this for some time and think I for the first time see it too. I don't know what is going on but wanted to let you know that we are indeed looking at it.
Henrik
(I don't have enough points to comment so having to do this as an answer)
Henrik, I have this same problem. If I ftp on to the box I can see that the web.config has the correct connection string but it fails as trying to use the username in the SQLServerDBConnectionString property (OoWUqr****Login). Is it possible you could let me know in what order it is looking for connection strings and where?
And if it can't stop it using the other user is there a way I can permission them for the correct database through mobile services?
Thanks
F

Sql or Connection string issue ? asp.net mvc 4 tutorials

Hi I'm having a continuous issues when it comes to database creation and access when trying out mvc 4 tutorials. The latest for example http://www.asp.net/mvc/tutorials/mvc-4. I've got to the point where I'm trying to use EF Code First Migrations to migrate some changes to the model classes so the change is applied to the database. And dummy data seeded to the database.
When I run "update-database" after entering "add-migration Initial" I get ....
Cannot open database "_Movies.mdf" requested by the login. The login failed.
Login failed for user 'USER\me'
When I check SQL server management studio, my name is listed in the Logins folder, within Security folder. Is there anything else I can check ? Still learning, so please explain answers.
Connection string if needed...
<add name="MovieDBContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=_Movies.mdf;AttachDbFilename=|DataDirectory|\_Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

Migrating MVC site from local to Azure, having SQL problems

So I have been trying to migrate my site from running locally to on Azure, I have been having a lot of problems, I've been stuck on this one specifically for a very long time, hoping someone here can help:
I migrated all my databases to Azure SQL using the Task -> Create Query, then running that Query in the Azure SQL. It changed the structure a bit (now all the tables are in one database, and they all start with "dbo.".
The error I am currently getting when I try to my site is:
"The connection string 'UnitsDBContext' in the application's configuration file does not contain the required providerName attribute."
My Connection Strings:
<connectionStrings>
<add name="UnitsDBContext"
connectionString="Server=tcp:cbf8vc1b7x.database.windows.net,1433;Database=Database;User ID=username;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
providerName="System.Data.SqlClient"
/>
<add name="DefaultConnection"
connectionString ="Server=tcp:cbf8vc1b7x.database.windows.net,1433;Database=Database;User ID=username;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
providerName = "System.Data.SqlClient;" />
</connectionStrings>
The error or a similar one occurs every time I go to a page that uses a database, though other pages load fine. Anyone have a suggestion?
Just to add to the thoughts from Ben Narube: -
You can try logging the connection string to make sure that what is running is actually correct. I have provided a Log4Net example (be sure to remove this afterwards and delete the file because you don't want connection strings lying around):
m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType );
foreach( var connectionString in System.Configuration.ConfigurationManager.ConnectionStrings )
{
string outString = connectionString.ToString();
m_log.Debug( outString );
}
Do you have a web.release.config or other config that is replacing this connection string? For example with a xdt:Locator="Match(name)
Are there any funny characters in your connection string in web.config? Maybe take a look in a hex editor to see if there are.

Azure Publish Database First Connection String Not Working

I have gone through the steps to publish my web app using database first on the azure portal.
However, when I publish I get this error message:
Code generated using the T4 templates for Database First and Model
First development may not work correctly if used in Code First mode.
To continue using Database First or Model First ensure that the Entity
Framework connection string is specified in the config file of
executing application. To use these classes, that were generated from
Database First or Model First, with Code First add any additional
configuration using attributes or the DbModelBuilder API and then
remove the code that throws this exception.
My connection string in the web.config after it has been modified by publish:
<add name="MySiteEntities" connectionString="metadata=res://*/MySite.csdl|res://*/MySite.ssdl|res://*/MySite.msl;provider=System.Data.SqlClient;provider connection string="data source=tcp:**********.database.windows.net,****;initial catalog=MySite;user id=username#**********;password=*******;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
My context (generated by edmx):
public partial class MySiteEntities : DbContext
{
public MySiteEntities()
: base("name=MySiteEntities")
{
}
...
I am very confused becuase it seems like entity framework is trying to use code first rather than database first.
UPDATE:
I just tried using the same connection string locally and the web app seems to run fine. The web app does connect to the remote database fine. It is only when I publish to azure it fails.
Read my answer to a similar question at Entity framework work locally but not on azure.
If you made the same "mistake" I did, this is what's happening ... the Azure-deployed app isn't finding your connection string "MySiteEntities" inside your web.config. Instead, at the time your created your Azure Web Site (or Cloud Service or whatever), you created an associated Azure SQL Database and gave its connection string the exact same name, "MySiteEntities". This latter connection string is a "plain" connection string without Model/Database-first metadata references, and so is being treated as a Code-First connection by EF, which then complains about the conflict. See Code First vs. Database First for an explanation of this distinction.
It should be:
<connectionStrings>
<add name="MyDatabaseModelEntities"
connectionString="metadata=res://*/MyDBModel.csdl|res://*/MyDBModel.ssdl|res://*/MyDBModel.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source=<provideServerName>.database.windows.net;
Initial Catalog=MyDatabase;
Integrated Security=False;
User ID=<provideUserID>;
Password=providePassword>;
MultipleActiveResultSets=True;
Encrypt=True;
TrustServerCertificate=False""
providerName="System.Data.EntityClient"/>
</connectionStrings>
I changed connection string to remote (Azure) on my local web.config, then remove all set connection strings during publishing and publish web.config. It rewrites remove web.config. Then return connection string on local web.config to local connection. It works fine now.

Categories