MSSQL Error 'The underlying provider failed on Open'

前端 未结 30 2548
时光取名叫无心
时光取名叫无心 2020-11-22 09:51

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

相关标签:
30条回答
  • 2020-11-22 10:33

    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.

    0 讨论(0)
  • 2020-11-22 10:34

    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.

    0 讨论(0)
  • 2020-11-22 10:34

    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=&quot;data source=.\SQlExpress;initial catalog=ShoppingCartAdmin;Integrated Security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" 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=&quot;data source=MACHINE_Name\SQlExpress;initial catalog=ShoppingCartAdmin;persist security info=True;user id=sa;password=notmyrealpassword;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    

    Other common mistakes could be:

    1. Database service could be stopped
    2. Data Source attributes pointing to a local database with Windows authentication and hosted in IIS
    3. Username and password could be wrong.
    0 讨论(0)
  • 2020-11-22 10:34

    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.

    0 讨论(0)
  • 2020-11-22 10:37

    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!

    0 讨论(0)
  • 2020-11-22 10:37

    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);
        }
    

    0 讨论(0)
提交回复
热议问题