I was using an .mdf for connecting to a database and entityClient. Now I want to change the connection string so that there will be no .mdf file.
Is the following connectionString correct?
<connectionStrings>
<!--<add name="conString" connectionString="metadata=res://*/conString.csdl|res://*/conString.ssdl|res://*/conString.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQL2008;AttachDbFilename=|DataDirectory|\NData.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />-->
<add name="conString" connectionString="metadata=res://*/conString.csdl|res://*/conString.ssdl|res://*/conString.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQL2008;Initial Catalog=NData;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
Because I always get the error:
The underlying provider failed on Open
I had this error and found a few solutions:
Looking at your connection string, it looks valid. I found this blog post, the problem here is that they were using Integrated Security. If you are running on IIS, your IIS user needs access to the database.
If you are using Entity Framework with Transactions, Entity Framework automatically opens and closes a connection with each database call. So when using transactions, you are attempting to spread a transaction out over multiple connections. This elevates to MSDTC.
(See this reference for more information.)
Changing my code to the following fixed it:
using (DatabaseEntities context = new DatabaseEntities())
{
context.Connection.Open();
// the rest
}
context.Connection.Open() didn't help solving my problem so I tried enabling "Allow Remote Clients" in DTC config, no more error.
In windows 7 you can open the DTC config by running dcomcnfg, Component Services -> Computers -> My Computer -> Distributed Transaction Coordinator -> Right click to Local DTC -> Security.
You should see innerException to see what the inner cause of throwing of
error is.
In my case, the original error was:
Unable to open the physical file "D:\Projects2\xCU\xCU\App_Data\xCUData_log.ldf". Operating system error 5: "5(Access is denied.)".
An attempt to attach an auto-named database for file D:\Projects2\xCU\xCU\App_Data\xCUData.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
which solved by giving full permission to current user for accessing related mdf and ldf files using files' properties.
I found the problem was that I had the server path within the connection string in one of these variants:
SERVER\SQLEXPRESS
SERVER
When really I should have:
.\SQLEXPRESS
For some reason I got the error whenever it had difficulty locating the instance of SQL.
This is common issue only. Even I have faced this issue. On the development machine, configured with Windows authentication, it is worked perfectly:
<add name="ShoppingCartAdminEntities" connectionString="metadata=res://*/ShoppingCartAPIModel.csdl|res://*/ShoppingCartAPIModel.ssdl|res://*/ShoppingCartAPIModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQlExpress;initial catalog=ShoppingCartAdmin;Integrated Security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
Once hosted in IIS with the same configuration, I got this error:
The underlying provider failed on Open
It was solved changing connectionString in the configuration file:
<add name="MyEntities" connectionString="metadata=res://*/ShoppingCartAPIModel.csdl|res://*/ShoppingCartAPIModel.ssdl|res://*/ShoppingCartAPIModel.msl;provider=System.Data.SqlClient;provider connection string="data source=MACHINE_Name\SQlExpress;initial catalog=ShoppingCartAdmin;persist security info=True;user id=sa;password=notmyrealpassword;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
Other common mistakes could be:
Database service could be stopped
Data Source attributes pointing to a local database with Windows authentication and hosted in IIS
Username and password could be wrong.
When you receive this exception, make sure to expand the detail and look at the inner exception details as it will provide details on why the login failed. In my case the connection string contained a user that did not have access to my database.
Regardless of whether you use Integrated Security (the context of the logged in Windows User) or an individual SQL account, make sure that the user has proper access under 'Security' for the database you are trying to access to prevent this issue.
The SQL Server Express service were not set tostart automatically.
1) Go to control panel
2) Administrative Tools
3) Service
4) Set SQL Server express to start automatically by clicking on it
5) Right click and start the service
I hope that will help.
I had a similar issue with the SQL Server Express Edition on Windows Server 2003. I simply added the network service as a user in the database security.
This can also happen if you restore a database and the user already exists with different schema, leaving you unable to assign the correct permissions.
To correct this run:
USE your_database
EXEC sp_change_users_login 'Auto_Fix', 'user', NULL, 'cf'
GO
EXEC sp_change_users_login 'update_one', 'user', 'user'
GO
I posted a similar issue here, working with a SQL 2012 db hosted on Amazon RDS. The problem was in the connection string - I had "Application Name" and "App" properties in there. Once I removed those, it worked.
Entity Framework 5 and Amazon RDS - "The underlying provider failed on Open."
Make sure that each element value in the connection string being supplied is correct. In my case, I was getting the same error because the name of the catalog (database name) specified in the connection string was incorrect.
I had a similar issue with exceptions due to the connection state, then I realized I had my domain service class variable marked as static (by mistake).
My guess is that once the service library is loaded into memory, each new call ends up using the same static variable value (domain service instance), causing conflicts via the connection state.
I think also that each client call resulted in a new thread, so multiple threads accessing the same domain service instance equated to a train wreck.
I had the same problem but what worked for me was removing this from the Connection String:
persist security info=True
I had a similar error with the inner exception as below:
operation is not valid for the state of the transaction
I could resolve it by enabling DTC security settings.
Go To Properties of DTC, under Security Tab, check the below
Network DTC Access
Allow RemoteClients
Transaction Manager Communication
Allow Inbound
Allow Outbound
If you happen to get this error on an ASP.NET web application, in addition to other things mentioned check the following:
Database User Security Permissions (which users are allowed access to your database.
Check your application pool in IIS and make sure it's the correct one that is allowed access to your database.
I got rid of this by resetting IIS, but still using Integrated Authentication in the connection string.
Defining a new Windows Firewall rule for SQL Server (and for port 1433) on the server machine solves this error (if your servername, user login name or password is not wrong in your connection string...).
NONE of the answers worked for me
I think that some of us all make silly mistakes, there are 100 ways to fail ...
My issue was new project, I setup all the configuration in another project, but the caller was a Web Api project in which I had to copy the same connection string in the Web api project.
I think this is crazy considering I was not even newing up dbcontext or anything from the web api.
Otherwise the class library was trying to look for a Database named
TokenApi.Core.CalContext
of which my project is named TokenApi.Core and the CalContext is the name of the connection string and the file name
I was searching all over the web for this problem. I had the wrong name in the connection string, please check you connection string in web.config. I had name="AppTest" but it should have been name="App".
In my AppTestContext.cs file I had:
public AppTestContext() : this("App") { }
Wrong connection string:
<add connectionString="Data Source=127.0.0.1;Initial Catalog=AppTest;Integrated Security=SSPI;MultipleActiveResultSets=True" name="AppTest" providerName="System.Data.SqlClient" />
Right connection string:
<add connectionString="Data Source=127.0.0.1;Initial Catalog=AppTest;Integrated Security=SSPI;MultipleActiveResultSets=True" name="App" providerName="System.Data.SqlClient" />
A common mistake that I did because I was moving application from once pc to another and none of the above worked was that I forgot to copy the connection string to both App.Config and Web.Config!
I had a similar problem: In my test-cases executions I always got this error. I found out, that my "Distributed Transaction Service" was not started (run: services.msc -> start "Distributed Transaction Service" (best to set it to start automatic)). After I did that, it worked like a charm...
I was also facing the same issue. Now I have done it by removing the user name and password from the connection string.
For me it was just a simple mistake:
I used Amazon EC2, and I used my elastic IP address in the connection string, but when I changed IP addresses I forgot to update my connection string.
I had this error suddenly happen out of the blue on one of our sites. In my case, it turned out that the SQL user's password had expired! Unticking the password expiration box in SQL Server Management Studio did the trick!
I had the same issue few days ago, using "Integrated Security=True;" in the connection string you need to run the application pool identity under "localsystem" Sure this is not recommended but for testing it does the job.
This is how you can change the identity in IIS 7:
http://www.iis.net/learn/manage/configuring-security/application-pool-identities
In IIS set the App Pool Identity As Service Account user or Administrator Account or ant account which has permission to do the operation on that DataBase.
In my case I had a mismatch between the connection string name I was registering in the context's constructor vs the name in my web.config. Simple mistake caused by copy and paste :D
public DataContext()
: base(nameOrConnectionString: "ConnStringName")
{
Database.SetInitializer<DataContext>(null);
}
I had this problem because the Application Pool login this app was running under had changed.
In IIS:
Find the Application pool by clicking on your site and going to Basic Settings.
Go to Application Pools.
Click on your site's application pool.
Click on Advanced Settings.
In Identity, enter account login and password.
Restart your site and try again.
I have solved this way.
Step 1:
Open Internet Information Service Manager
Step 2:
Click on Application Pools in left navigation tree.
Step 3:
Select your version Pool. In my case, I am using ASP .Net v4.0. If you dont have this version, select DefaultAppPool.
Step 4:
Right click on step 3, and select advanced settings.
Step 5:
Select Identity in properties window and click the button to change the value.
Step 6:
Select Local System in Built-in accounts combo box and click ok.
That's it. Now run your application. Everything works well.
Codeproject Solution : the-underlying-provider-failed-on-open
I get this error when call async EF method from sync Main console (await was skipped).
Because async opening a connection was for an already disposed data context.
Solve: call async EF methods correctly
Related
Attempting to publish a website to my host which contains my custom database and the default like so:
<add name="PortfolioContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Portfolio;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
When publishing the site I can't login which I assume is down to because the DefaultConnection db isn't being created (containing the membership user tables), I might be wrong? I can generate the script for my custom database in Management studio but DefaultConnection doesn't even appear to allow me to manually create tables associated.
My error log displays this
2016-02-02 12:28:35 W3SVC30 217.194.213.135 GET /Login
ReturnUrl=%2Fdashboard 80 - 95.147.124.217
Mozilla/5.0+(Windows+NT+10.0;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/48.0.2564.97+Safari/537.36
- 500 0 0 1198 416 203
Though I'm not sure what that means. Sorry this is my first attempt at publishing a site. Thanks
Edit:
Could not find a part of the path 'C:\HostingSpaces\hannahha\hannahhamlin.co.uk\wwwroot\Assets\Common\images'.
This is the error it displays. This leads to me a problem I knew i was going to have to confront anyway. How do I include necessary directories for publishing?
First, that's not an error log. That's IIS' default request log. All it tells you is that a client using a particular browser requested /Login via GET. In other words, nothing at all useful to this issue.
You need a real error, with a stacktrace. You can either turn off custom errors temporarily (<customErrors mode="Off" /> in your Web.config), install something like Elmah to log errors for you, or use remote debugging.
However, most likely, the connection string only works locally, and the error you would see is just a DB connection error. It's rare that the connection string you'd be using in development would match exactly with what would be required for production, so you should utilize config transforms to change it when publishing.
That's assuming of course you even have a database server set up in production to connect to. Visual Studio employs a lot of database magic to allow you to focus on your code rather than menial implementation details like connecting to a database. However, when you go live, the training wheels are off, and you're responsible for making sure everything necessary is wired up. If you don't have a database server, install one. If you don't have a database, create one. Then, just alter your connection string to connect to that.
UPDATE
The error message is so far removed from this question was originally about, I think you would be best served by creating a new question and deleting this one.
That said, I'm not sure what you're trying to achieve exactly, but anything under your project directory will be published along with your site (except certain special directories like App_Data). If the files you're referencing are in some directory outside of your project directory, they will not be included, and there's really not a way to include them. You can, however, create a build action that copies the files somewhere under your project, and by virtue of that, they should then be published.
I'm struggling with an issue that seemingly came out of nowhere for me.
I'm using A DbContext with a connection string specified from a Web.config file. It was working fine, until it wasn't. Now when I run my integration tests, my DbContext can't save changes and I get the "Login failed for user Microsoft\my#email.com" exception, where Microsoft\my#email.com is the email I have registered with my Microsoft account. The strange thing is is that my SQL server uses Windows authentication, and I don't specify a username to be used anywhere in my connections. I've debugged and verified that my context's connection string is accurate, so I have no idea why my connection would be attempting to connect with a username I never set, from an account that I don't link to anywhere in my code. I can also connect to my localhost db fine thru SSMS using Windows auth, so I know that isn't the issue. I don't recall explicitly changing anything, however something obviously got switched somewhere. See my connection string below.
<add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient"/>
Is there another place a connection might be attempting to pull information from, or something I may be missing in regards to my connection? Any guidance here is greatly appreciated!
I needed to run Visual Studio as an administrator in order for my program to have access to localhost, and the user it used was coming from my Microsoft account. Hope this helps someone else running in to this!
Slightly at my wits end. Built an application, running fine locally. Migrated my database to MSSQL without issue, uploaded the site, can't seem to get the application to connect to the database. Any page that accesses the database I get a generic error message.
I've tried all the separate combinations of connection strings I could think of using the Godaddy recommended connection strings. Perhaps I am overlooking something simple?
I'm using Entity Framework Code-First -- My context model is called CombosContext.
<add name="CombosContext" connectionString=" Server=jelatin.db.9508732.hostedresource.com; Database=jelatin; User ID=jelatin; Password=********; Trusted_Connection=False" providerName="System.Data.SqlClient" />
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString=" Server=jelatin.db.9508732.hostedresource.com; Database=jelatin; User ID=jelatin; Password=********; Trusted_Connection=False" providerName="System.Data.SqlClient" />
Server: jelatin.db.9508732.hostedresource.com
DB name: jelatin
user: jelatin
Table: Comboes
I'm unfamiliar with GoDaddy hosted SQL, but usually a connection string to MS SQL Server uses "Data Source" instead of "Server" and "Initial Catalog" instead of "Database".
UPDATE
I didn't realize Server and Database were allowed options in the Connection String. Sorry for the confusion.
Regarding the database itself - are you letting EF create the database? Does the user have permission on GoDaddy's system to create a database?
If you have already created the database, did you populate anything? I have found that EF Code First won't correctly populate the database if the database exists and the metadata table doesn't. If you can, try copying your local database up to GoDaddy, and see if the connection works.
Finally, for your generic error message - is it coming back in a 500 error? If so, have you tried using either IE or Chrome's dev tools to inspect the response? Better error information is usually hidden in there.
I to do connection and have error.
from u in db.aspnet_users where u.UserName.Equals(email) select u
error:
Cannot open database "AdvSkidki" requested by the login. The login
failed. Login failed for user 'USER\User'.
I can not understand where the data user / user.
I have only <add name="AdvSkidkiEntities" connectionString="metadata=res://*/Entities.Model.csdl|res://*/Entities.Model.ssdl|res://*/Entities.Model.msl;provider=System.Data.SqlClient;provider connection string="data source=USER\SQLEXPRESS;initial catalog=AdvSkidki;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> in web.config in myproject.
query to the database is in myproject.Domain.Conrete
update:
if the *. edmx located in the main project - all works well
Because you have indicated integrated security=True; in your connection string, it means that the Application Pool running your app needs to be granted access to your database.
Either add USER\User as a login to the SQLExpress server, with sufficient access to the AdvSkidki database, or change the identity of your App Pool to one that does have access to the database.
nonnb pointed the problem, you have 3 solutions :
Change WebSite (app pool) identity as already said : you must have access to the IIS server (only IIS 6.0 and greater have Application Pools). There's a lot of articles and Microsoft docs about it, check this one for IIS 6, and this one for IIS 7
Specificy a username and password in the connection string instead of Integrated
security
Impersonate the application/code section and continue with Integrated
security, see how to impersonate a web application and how to impersonate a code section
I am successfully running tests through the the WCF Test Client, until I try to pull in data with Entity Framework.
To make sure I'm not doing anything stupid, I downloaded the sample code from this tutorial, which is doing something similar: http://www.codeproject.com/KB/WCF/WCFandEF.aspx
...and when I run it, I get the same error in a similar place:
var productEntity = (from p in context.ProductEntities
where p.ProductID == id
select p).FirstOrDefault();
The error is
The underlying provider failed on Open.
I can open the database fine from a "normal application" with the same connection string, it seems to be specific accessing the DB from the WCF test client.
Research here and on Google for "The underlying provider failed on Open." usual indicates that it's a connection string problem, but I'm pretty sure it's not in this case.
So now I expect it's some sort of permissions problem.
I am using SQL Server and Windows 7, with visual studio 2010.
I have been banging my head since yesterday, so any help or protective head gear appreciated.
Edited to include connection string
<add name="NorthwindEntities"
connectionString="metadata=res://*/Northwind.csdl|res://*/Northwind.ssdl|res://*/Northwind.msl;provider=System.Data.SqlClient;provider
connection string="Data Source=localhost;Initial Catalog=Northwind;User ID=sa;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
This error means 100% a problem in your connection string.
A good way to create a surely working connection string is to create a new(dummy) project, add an Entity Framework Data Model to it, select "Generate Model from Database", select your required connection, and click "Test Conntection" to be sure it works.
Make sure ""Save entity conection settings in App.Config as:..." is ticked.
Than, in you web/app.Config, you have a Connection String you can copy+paste to your own project
Have you tried inserting the database password into the connection string?
It worked for me.
Reference:
http://stack247.wordpress.com/2011/03/02/entity-framework-exception-the-underlying-provider-failed-on-open/
Well I had the same error for days... actualy for two days and Yes I found the solution Alhamdulillah..
As for me... I had Windows Authentication set on my Entity Service Connection.. So What I did was I went to IIS 7 Application Pool Advanced Settings for the web service... Changed the Identity from 'ApplicationPoolIdentity' to 'Network Service' and I setup my current user name and password as well.
I might face some other issues later on, still I would say a Good Start :-)!
I was remove "integrated security=True;" solved this way.